Search in sources :

Example 46 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project xwiki-platform by xwiki.

the class Package method Import.

/**
 * Load this package in memory from an InputStream. It may be installed later using {@link #install(XWikiContext)}.
 *
 * @param file an InputStream of a zipped package file
 * @param context current XWikiContext
 * @return an empty string, useless.
 * @throws IOException while reading the ZipFile
 * @throws XWikiException when package content is broken
 * @since 2.3M2
 */
public String Import(InputStream file, XWikiContext context) throws IOException, XWikiException {
    ZipArchiveInputStream zis;
    ArchiveEntry entry;
    Document description = null;
    try {
        zis = new ZipArchiveInputStream(file, XAR_FILENAME_ENCODING, false);
        List<XWikiDocument> docsToLoad = new LinkedList<XWikiDocument>();
        /*
             * Loop 1: Cycle through the zip input stream and load out all of the documents, when we find the
             * package.xml file we put it aside to so that we only include documents which are in the file.
             */
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory() || (entry.getName().indexOf("META-INF") != -1)) {
                // (we use that directory to put meta data such as LICENSE/NOTICE files.)
                continue;
            } else if (entry.getName().compareTo(DefaultPackageFileName) == 0) {
                // The entry is the manifest (package.xml). Read this differently.
                description = fromXml(new CloseShieldInputStream(zis));
            } else {
                XWikiDocument doc = null;
                try {
                    doc = readFromXML(new CloseShieldInputStream(zis));
                } catch (Throwable e) {
                    LOGGER.warn("Failed to parse document [{}] from XML during import, thus it will not be installed. " + "The error was: " + ExceptionUtils.getRootCauseMessage(e));
                    // It will be listed in the "failed documents" section after the import.
                    addToErrors(entry.getName().replaceAll("/", "."), context);
                    continue;
                }
                // if no filters throw exceptions, add it to the list to import.
                try {
                    this.filter(doc, context);
                    docsToLoad.add(doc);
                } catch (ExcludeDocumentException e) {
                    LOGGER.info("Skip the document '" + doc.getDocumentReference() + "'");
                }
            }
        }
        // Make sure a manifest was included in the package...
        if (description == null) {
            throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Could not find the package definition");
        }
        /*
             * Loop 2: Cycle through the list of documents and if they are in the manifest then add them, otherwise log
             * a warning and add them to the skipped list.
             */
        for (XWikiDocument doc : docsToLoad) {
            if (documentExistInPackageFile(doc.getFullName(), doc.getLanguage(), description)) {
                this.add(doc, context);
            } else {
                LOGGER.warn("document " + doc.getDocumentReference() + " does not exist in package definition." + " It will not be installed.");
                // It will be listed in the "skipped documents" section after the
                // import.
                addToSkipped(doc.getFullName(), context);
            }
        }
        updateFileInfos(description);
    } catch (DocumentException e) {
        throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Error when reading the XML");
    }
    return "";
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) DocumentException(org.dom4j.DocumentException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) DOMDocument(org.dom4j.dom.DOMDocument) Document(org.dom4j.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) LinkedList(java.util.LinkedList) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 47 with ArchiveEntry

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

the class EpubExtractor method proccessHypensApache.

public static void proccessHypensApache(String input, String output) throws Exception {
    LOG.d("proccessHypens2", input, output);
    FileInputStream inputStream = new FileInputStream(new File(input));
    ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
    ArchiveEntry nextEntry = null;
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(output)));
    zos.setLevel(0);
    while ((nextEntry = zipInputStream.getNextEntry()) != null) {
        if (TempHolder.get().loadingCancelled) {
            break;
        }
        String name = nextEntry.getName();
        String nameLow = name.toLowerCase();
        if (!name.endsWith("container.xml") && (nameLow.endsWith("html") || nameLow.endsWith("htm") || nameLow.endsWith("xml"))) {
            LOG.d("nextEntry HTML cancell", TempHolder.get().loadingCancelled, name);
            ByteArrayOutputStream hStream = Fb2Extractor.generateHyphenFile(new InputStreamReader(zipInputStream));
            Fb2Extractor.writeToZipNoClose(zos, name, new ByteArrayInputStream(hStream.toByteArray()));
        } else {
            LOG.d("nextEntry cancell", TempHolder.get().loadingCancelled, name);
            Fb2Extractor.writeToZipNoClose(zos, name, zipInputStream);
        }
    }
    zipInputStream.close();
    inputStream.close();
    zos.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 48 with ArchiveEntry

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

the class EpubExtractor method getBookCover.

@Override
public byte[] getBookCover(String path) {
    byte[] cover = null;
    try {
        InputStream inputStream = new FileInputStream(new File(path));
        ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
        ArchiveEntry nextEntry = null;
        String coverName = null;
        String coverResource = null;
        while (coverName == null && (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 ("meta".equals(xpp.getName()) && "cover".equals(xpp.getAttributeValue(null, "name"))) {
                            coverResource = xpp.getAttributeValue(null, "content");
                        }
                        if (coverResource != null && "item".equals(xpp.getName()) && coverResource.equals(xpp.getAttributeValue(null, "id"))) {
                            coverName = xpp.getAttributeValue(null, "href");
                            if (coverName != null && coverName.endsWith(".svg")) {
                                coverName = null;
                            }
                            break;
                        }
                    }
                    eventType = xpp.next();
                }
            }
        }
        if (coverName != null) {
            zipInputStream.close();
            inputStream.close();
            inputStream = new FileInputStream(new File(path));
            zipInputStream = new ZipArchiveInputStream(inputStream);
            while ((nextEntry = zipInputStream.getNextEntry()) != null) {
                String name = nextEntry.getName();
                if (name.contains(coverName)) {
                    cover = BaseExtractor.getEntryAsByte(zipInputStream);
                    break;
                }
            }
        }
        if (cover == null) {
            zipInputStream.close();
            inputStream.close();
            inputStream = new FileInputStream(new File(path));
            zipInputStream = new ZipArchiveInputStream(inputStream);
            while ((nextEntry = zipInputStream.getNextEntry()) != null) {
                String name = nextEntry.getName().toLowerCase(Locale.US);
                if (name.endsWith(".jpeg") || name.endsWith(".jpg") || name.endsWith(".png")) {
                    if (name.contains("cover")) {
                        cover = BaseExtractor.getEntryAsByte(zipInputStream);
                        break;
                    }
                }
            }
        }
        if (cover == null) {
            zipInputStream.close();
            inputStream.close();
            inputStream = new FileInputStream(new File(path));
            zipInputStream = new ZipArchiveInputStream(inputStream);
            while ((nextEntry = zipInputStream.getNextEntry()) != null) {
                String name = nextEntry.getName().toLowerCase(Locale.US);
                if (name.endsWith(".jpeg") || name.endsWith(".jpg") || name.endsWith(".png")) {
                    cover = BaseExtractor.getEntryAsByte(zipInputStream);
                    break;
                }
            }
        }
        zipInputStream.close();
        inputStream.close();
    } catch (Exception e) {
        LOG.e(e);
    }
    return cover;
}
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 49 with ArchiveEntry

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

the class CompressBackupUtilTest method testFolderArchived.

@Test
public void testFolderArchived() throws Exception {
    File src = new File(randName);
    src.mkdir();
    FileWriter fw = new FileWriter(new File(src, "1.txt"));
    fw.write("12345");
    fw.close();
    fw = new FileWriter(new File(src, "2.txt"));
    fw.write("12");
    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 entry1 = tai.getNextEntry();
    ArchiveEntry entry2 = tai.getNextEntry();
    if (entry1.getName().compareTo(entry2.getName()) > 0) {
        // kinda sort them lol
        ArchiveEntry tmp = entry1;
        entry1 = entry2;
        entry2 = tmp;
    }
    Assert.assertNotNull("No entry found in destination archive", entry1);
    Assert.assertEquals("Entry has wrong size", 5, entry1.getSize());
    System.out.println(entry1.getName());
    Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName());
    System.out.println(entry2.getName());
    Assert.assertEquals("Entry has wrong size", 2, entry2.getSize());
    Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName());
}
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 50 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project AozoraEpub3 by hmdev.

the class AozoraEpub3 method getTextInputStream.

/**
 * 入力ファイルからStreamオープン
 *
 * @param srcFile
 * @param ext
 * @param imageInfoReader
 * @param txtIdx テキストファイルのZip内の位置
 * @return テキストファイルのストリーム (close()は呼び出し側ですること)
 * @throws RarException
 */
public static InputStream getTextInputStream(File srcFile, String ext, ImageInfoReader imageInfoReader, String[] textEntryName, int txtIdx) throws IOException, RarException {
    if ("txt".equals(ext)) {
        return new FileInputStream(srcFile);
    } else if ("zip".equals(ext) || "txtz".equals(ext)) {
        // Zipなら最初のtxt
        ZipArchiveInputStream zis = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), 65536), "MS932", false);
        ArchiveEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String entryName = entry.getName();
            if (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt") && txtIdx-- == 0) {
                if (imageInfoReader != null)
                    imageInfoReader.setArchiveTextEntry(entryName);
                if (textEntryName != null)
                    textEntryName[0] = entryName;
                return zis;
            }
        }
        LogAppender.append("zip内にtxtファイルがありません: ");
        LogAppender.println(srcFile.getName());
        return null;
    } else if ("rar".equals(ext)) {
        // tempのtxtファイル作成
        Archive archive = new Archive(srcFile);
        try {
            FileHeader fileHeader = archive.nextFileHeader();
            while (fileHeader != null) {
                if (!fileHeader.isDirectory()) {
                    String entryName = fileHeader.getFileNameW();
                    if (entryName.length() == 0)
                        entryName = fileHeader.getFileNameString();
                    entryName = entryName.replace('\\', '/');
                    if (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt") && txtIdx-- == 0) {
                        if (imageInfoReader != null)
                            imageInfoReader.setArchiveTextEntry(entryName);
                        if (textEntryName != null)
                            textEntryName[0] = entryName;
                        // tmpファイルにコピーして終了時に削除
                        File tmpFile = File.createTempFile("rarTmp", "txt");
                        tmpFile.deleteOnExit();
                        FileOutputStream fos = new FileOutputStream(tmpFile);
                        InputStream is = archive.getInputStream(fileHeader);
                        try {
                            IOUtils.copy(is, fos);
                        } finally {
                            is.close();
                            fos.close();
                        }
                        return new BufferedInputStream(new FileInputStream(tmpFile), 65536);
                    }
                }
                fileHeader = archive.nextFileHeader();
            }
        } finally {
            archive.close();
        }
        LogAppender.append("rar内にtxtファイルがありません: ");
        LogAppender.println(srcFile.getName());
        return null;
    } else {
        LogAppender.append("txt, zip, rar, txtz, cbz のみ変換可能です: ");
        LogAppender.println(srcFile.getPath());
    }
    return null;
}
Also used : Archive(com.github.junrar.Archive) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) FileHeader(com.github.junrar.rarfile.FileHeader) File(java.io.File) FileInputStream(java.io.FileInputStream)

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