Search in sources :

Example 21 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project xodus by JetBrains.

the class CompressBackupUtilTest method testFileArchived.

@Test
public void testFileArchived() throws Exception {
    File src = new File(randName + ".txt");
    FileWriter fw = new FileWriter(src);
    fw.write("12345");
    fw.close();
    CompressBackupUtil.tar(src, dest);
    Assert.assertTrue("No destination archive created", dest.exists());
    TarArchiveInputStream tai = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest))));
    ArchiveEntry entry = tai.getNextEntry();
    Assert.assertNotNull("No entry found in destination archive", entry);
    Assert.assertEquals("Entry has wrong size", 5, entry.getSize());
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileWriter(java.io.FileWriter) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 22 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.

the class EpubExtractor method getBookOverview.

@Override
public String getBookOverview(String path) {
    String info = "";
    try {
        final File file = new File(path);
        InputStream inputStream = new FileInputStream(file);
        ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
        ArchiveEntry nextEntry = null;
        while ((nextEntry = zipInputStream.getNextEntry()) != null) {
            String name = nextEntry.getName().toLowerCase();
            if (name.endsWith(".opf")) {
                XmlPullParser xpp = XmlParser.buildPullParser();
                xpp.setInput(zipInputStream, "utf-8");
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if ("dc:description".equals(xpp.getName()) || "dcns:description".equals(xpp.getName())) {
                            info = xpp.nextText();
                            break;
                        }
                    }
                    if (eventType == XmlPullParser.END_TAG) {
                        if ("metadata".equals(xpp.getName())) {
                            break;
                        }
                    }
                    eventType = xpp.next();
                }
            }
        }
        zipInputStream.close();
        inputStream.close();
    } catch (Exception e) {
        LOG.e(e);
    }
    return info;
}
Also used : ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 23 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.

the class EpubExtractor method getBookMetaInformation.

@Override
public EbookMeta getBookMetaInformation(String path) {
    final File file = new File(path);
    try {
        InputStream inputStream = new FileInputStream(file);
        ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
        ArchiveEntry nextEntry = null;
        String title = null;
        String author = null;
        String subject = "";
        String series = null;
        String number = null;
        String lang = null;
        while ((nextEntry = zipInputStream.getNextEntry()) != null) {
            String name = nextEntry.getName().toLowerCase();
            if (name.endsWith(".opf")) {
                XmlPullParser xpp = XmlParser.buildPullParser();
                xpp.setInput(zipInputStream, "utf-8");
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if ("dc:title".equals(xpp.getName()) || "dcns:title".equals(xpp.getName())) {
                            title = xpp.nextText();
                        }
                        if ("dc:creator".equals(xpp.getName()) || "dcns:creator".equals(xpp.getName())) {
                            author = xpp.nextText();
                        }
                        if ("dc:subject".equals(xpp.getName()) || "dcns:subject".equals(xpp.getName())) {
                            subject = xpp.nextText() + "," + subject;
                        }
                        if (lang == null && ("dc:language".equals(xpp.getName()) || "dcns:language".equals(xpp.getName()))) {
                            lang = xpp.nextText();
                        }
                        if ("meta".equals(xpp.getName())) {
                            String nameAttr = xpp.getAttributeValue(null, "name");
                            if ("calibre:series".equals(nameAttr)) {
                                series = xpp.getAttributeValue(null, "content");
                            } else if ("calibre:series_index".equals(nameAttr)) {
                                number = xpp.getAttributeValue(null, "content");
                                if (number != null) {
                                    number = number.replace(".0", "");
                                }
                            }
                        }
                    }
                    if (eventType == XmlPullParser.END_TAG) {
                        if ("metadata".equals(xpp.getName())) {
                            break;
                        }
                    }
                    eventType = xpp.next();
                }
            }
        }
        zipInputStream.close();
        inputStream.close();
        if (AppState.get().isFirstSurname) {
            author = TxtUtils.replaceLastFirstName(author);
        }
        EbookMeta ebookMeta = new EbookMeta(title, author, series, subject.replaceAll(",$", ""));
        try {
            if (number != null) {
                ebookMeta.setsIndex(Integer.parseInt(number));
            }
        } catch (Exception e) {
            title = title + " [" + number + "]";
            ebookMeta.setTitle(title);
            LOG.d(e);
        }
        ebookMeta.setLang(lang);
        // ebookMeta.setPagesCount((int) size / 1024);
        return ebookMeta;
    } catch (Exception e) {
        return EbookMeta.Empty();
    }
}
Also used : ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 24 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.

the class ChangeSet method addAddition.

/**
 * Adds an addition change.
 *
 * @param pChange
 *            the change which should result in an addition
 */
private void addAddition(final Change pChange) {
    if (Change.TYPE_ADD != pChange.type() || pChange.getInput() == null) {
        return;
    }
    if (!changes.isEmpty()) {
        for (final Iterator<Change> it = changes.iterator(); it.hasNext(); ) {
            final Change change = it.next();
            if (change.type() == Change.TYPE_ADD && change.getEntry() != null) {
                final ArchiveEntry entry = change.getEntry();
                if (entry.equals(pChange.getEntry())) {
                    if (pChange.isReplaceMode()) {
                        it.remove();
                        changes.add(pChange);
                        return;
                    }
                    // do not add this change
                    return;
                }
            }
        }
    }
    changes.add(pChange);
}
Also used : ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Example 25 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.

the class ChangeSetPerformer method perform.

/**
 * Performs all changes collected in this ChangeSet on the input entries and
 * streams the result to the output stream.
 *
 * This method finishes the stream, no other entries should be added
 * after that.
 *
 * @param entryIterator
 *            the entries to perform the changes on
 * @param out
 *            the resulting OutputStream with all modifications
 * @throws IOException
 *             if an read/write error occurs
 * @return the results of this operation
 */
private ChangeSetResults perform(final ArchiveEntryIterator entryIterator, final ArchiveOutputStream out) throws IOException {
    final ChangeSetResults results = new ChangeSetResults();
    final Set<Change> workingSet = new LinkedHashSet<>(changes);
    for (final Iterator<Change> it = workingSet.iterator(); it.hasNext(); ) {
        final Change change = it.next();
        if (change.type() == Change.TYPE_ADD && change.isReplaceMode()) {
            copyStream(change.getInput(), out, change.getEntry());
            it.remove();
            results.addedFromChangeSet(change.getEntry().getName());
        }
    }
    while (entryIterator.hasNext()) {
        final ArchiveEntry entry = entryIterator.next();
        boolean copy = true;
        for (final Iterator<Change> it = workingSet.iterator(); it.hasNext(); ) {
            final Change change = it.next();
            final int type = change.type();
            final String name = entry.getName();
            if (type == Change.TYPE_DELETE && name != null) {
                if (name.equals(change.targetFile())) {
                    copy = false;
                    it.remove();
                    results.deleted(name);
                    break;
                }
            } else if (type == Change.TYPE_DELETE_DIR && name != null) {
                // don't combine ifs to make future extensions more easy
                if (name.startsWith(change.targetFile() + "/")) {
                    // NOPMD
                    copy = false;
                    results.deleted(name);
                    break;
                }
            }
        }
        if (copy && !isDeletedLater(workingSet, entry) && !results.hasBeenAdded(entry.getName())) {
            copyStream(entryIterator.getInputStream(), out, entry);
            results.addedFromStream(entry.getName());
        }
    }
    // Adds files which hasn't been added from the original and do not have replace mode on
    for (final Iterator<Change> it = workingSet.iterator(); it.hasNext(); ) {
        final Change change = it.next();
        if (change.type() == Change.TYPE_ADD && !change.isReplaceMode() && !results.hasBeenAdded(change.getEntry().getName())) {
            copyStream(change.getInput(), out, change.getEntry());
            it.remove();
            results.addedFromChangeSet(change.getEntry().getName());
        }
    }
    out.finish();
    return results;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Aggregations

ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)62 File (java.io.File)24 FileInputStream (java.io.FileInputStream)24 IOException (java.io.IOException)22 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)20 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)19 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)17 InputStream (java.io.InputStream)16 FileOutputStream (java.io.FileOutputStream)11 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)11 BufferedInputStream (java.io.BufferedInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Path (java.nio.file.Path)9 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)9 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)6 OutputStream (java.io.OutputStream)5 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4