Search in sources :

Example 1 with SevenZFile

use of org.apache.commons.compress.archivers.sevenz.SevenZFile in project structr by structr.

the class UnarchiveCommand method unarchive.

private void unarchive(final SecurityContext securityContext, final File file, final String parentFolderId) throws ArchiveException, IOException, FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    final InputStream is;
    Folder existingParentFolder = null;
    final String fileName = file.getName();
    try (final Tx tx = app.tx()) {
        // search for existing parent folder
        existingParentFolder = app.get(Folder.class, parentFolderId);
        String parentFolderName = null;
        String msgString = "Unarchiving file {}";
        if (existingParentFolder != null) {
            parentFolderName = existingParentFolder.getName();
            msgString += " into existing folder {}.";
        }
        logger.info(msgString, new Object[] { fileName, parentFolderName });
        is = file.getInputStream();
        tx.success();
        if (is == null) {
            getWebSocket().send(MessageBuilder.status().code(400).message("Could not get input stream from file ".concat(fileName)).build(), true);
            return;
        }
        tx.success();
    }
    final BufferedInputStream bufferedIs = new BufferedInputStream(is);
    switch(ArchiveStreamFactory.detect(bufferedIs)) {
        // 7z doesn't support streaming
        case ArchiveStreamFactory.SEVEN_Z:
            {
                int overallCount = 0;
                logger.info("7-Zip archive format detected");
                try (final Tx outertx = app.tx()) {
                    SevenZFile sevenZFile = new SevenZFile(file.getFileOnDisk());
                    SevenZArchiveEntry sevenZEntry = sevenZFile.getNextEntry();
                    while (sevenZEntry != null) {
                        try (final Tx tx = app.tx(true, true, false)) {
                            int count = 0;
                            while (sevenZEntry != null && count++ < 50) {
                                final String entryPath = "/" + PathHelper.clean(sevenZEntry.getName());
                                logger.info("Entry path: {}", entryPath);
                                if (sevenZEntry.isDirectory()) {
                                    handleDirectory(securityContext, existingParentFolder, entryPath);
                                } else {
                                    byte[] buf = new byte[(int) sevenZEntry.getSize()];
                                    sevenZFile.read(buf, 0, buf.length);
                                    try (final ByteArrayInputStream in = new ByteArrayInputStream(buf)) {
                                        handleFile(securityContext, in, existingParentFolder, entryPath);
                                    }
                                }
                                sevenZEntry = sevenZFile.getNextEntry();
                                overallCount++;
                            }
                            logger.info("Committing transaction after {} entries.", overallCount);
                            tx.success();
                        }
                    }
                    logger.info("Unarchived {} files.", overallCount);
                    outertx.success();
                }
                break;
            }
        // ZIP needs special treatment to support "unsupported feature data descriptor"
        case ArchiveStreamFactory.ZIP:
            {
                logger.info("Zip archive format detected");
                try (final ZipArchiveInputStream in = new ZipArchiveInputStream(bufferedIs, null, false, true)) {
                    handleArchiveInputStream(in, app, securityContext, existingParentFolder);
                }
                break;
            }
        default:
            {
                logger.info("Default archive format detected");
                try (final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(bufferedIs)) {
                    handleArchiveInputStream(in, app, securityContext, existingParentFolder);
                }
            }
    }
    getWebSocket().send(MessageBuilder.finished().callback(callback).data("success", true).data("filename", fileName).build(), true);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile) Tx(org.structr.core.graph.Tx) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) SevenZArchiveEntry(org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry) Folder(org.structr.web.entity.Folder)

Example 2 with SevenZFile

use of org.apache.commons.compress.archivers.sevenz.SevenZFile in project hutool by looly.

the class SevenZExtractor method extractInternal.

/**
 * 释放(解压)到指定目录
 *
 * @param targetDir 目标目录
 * @param filter    解压文件过滤器,用于指定需要释放的文件,null表示不过滤。当{@link Filter#accept(Object)}为true时释放。
 * @throws IOException IO异常
 */
private void extractInternal(File targetDir, Filter<ArchiveEntry> filter) throws IOException {
    Assert.isTrue(null != targetDir && ((false == targetDir.exists()) || targetDir.isDirectory()), "target must be dir.");
    final SevenZFile sevenZFile = this.sevenZFile;
    SevenZArchiveEntry entry;
    File outItemFile;
    while (null != (entry = this.sevenZFile.getNextEntry())) {
        if (null != filter && false == filter.accept(entry)) {
            continue;
        }
        outItemFile = FileUtil.file(targetDir, entry.getName());
        if (entry.isDirectory()) {
            // 创建对应目录
            // noinspection ResultOfMethodCallIgnored
            outItemFile.mkdirs();
        } else if (entry.hasStream()) {
            // 读取entry对应数据流
            FileUtil.writeFromStream(new Seven7EntryInputStream(sevenZFile, entry), outItemFile);
        } else {
            // 无数据流的文件创建为空文件
            FileUtil.touch(outItemFile);
        }
    }
}
Also used : SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile) SevenZArchiveEntry(org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry) File(java.io.File) SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile)

Example 3 with SevenZFile

use of org.apache.commons.compress.archivers.sevenz.SevenZFile in project tika by apache.

the class PackageParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    //lazily load the MediaTypeRegistry at parse time
    //only want to call getDefaultConfig() once, and can't
    //load statically because of the ForkParser
    TikaConfig config = context.get(TikaConfig.class);
    MediaTypeRegistry mediaTypeRegistry = null;
    if (config != null) {
        mediaTypeRegistry = config.getMediaTypeRegistry();
    } else {
        if (bufferedMediaTypeRegistry == null) {
            //buffer this for next time.
            synchronized (lock) {
                //now that we're locked, check again
                if (bufferedMediaTypeRegistry == null) {
                    bufferedMediaTypeRegistry = TikaConfig.getDefaultConfig().getMediaTypeRegistry();
                }
            }
        }
        mediaTypeRegistry = bufferedMediaTypeRegistry;
    }
    // Ensure that the stream supports the mark feature
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    TemporaryResources tmp = new TemporaryResources();
    ArchiveInputStream ais = null;
    try {
        ArchiveStreamFactory factory = context.get(ArchiveStreamFactory.class, new ArchiveStreamFactory());
        // At the end we want to close the archive stream to release
        // any associated resources, but the underlying document stream
        // should not be closed
        ais = factory.createArchiveInputStream(new CloseShieldInputStream(stream));
    } catch (StreamingNotSupportedException sne) {
        // Most archive formats work on streams, but a few need files
        if (sne.getFormat().equals(ArchiveStreamFactory.SEVEN_Z)) {
            // Rework as a file, and wrap
            stream.reset();
            TikaInputStream tstream = TikaInputStream.get(stream, tmp);
            // Seven Zip suports passwords, was one given?
            String password = null;
            PasswordProvider provider = context.get(PasswordProvider.class);
            if (provider != null) {
                password = provider.getPassword(metadata);
            }
            SevenZFile sevenz;
            if (password == null) {
                sevenz = new SevenZFile(tstream.getFile());
            } else {
                sevenz = new SevenZFile(tstream.getFile(), password.getBytes("UnicodeLittleUnmarked"));
            }
            // Pending a fix for COMPRESS-269 / TIKA-1525, this bit is a little nasty
            ais = new SevenZWrapper(sevenz);
        } else {
            tmp.close();
            throw new TikaException("Unknown non-streaming format " + sne.getFormat(), sne);
        }
    } catch (ArchiveException e) {
        tmp.close();
        throw new TikaException("Unable to unpack document stream", e);
    }
    updateMediaType(ais, mediaTypeRegistry, metadata);
    // Use the delegate parser to parse the contained document
    EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    try {
        ArchiveEntry entry = ais.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory()) {
                parseEntry(ais, entry, extractor, metadata, xhtml);
            }
            entry = ais.getNextEntry();
        }
    } catch (UnsupportedZipFeatureException zfe) {
        // If it's an encrypted document of unknown password, report as such
        if (zfe.getFeature() == Feature.ENCRYPTION) {
            throw new EncryptedDocumentException(zfe);
        }
        // Otherwise throw the exception
        throw new TikaException("UnsupportedZipFeature", zfe);
    } catch (PasswordRequiredException pre) {
        throw new EncryptedDocumentException(pre);
    } finally {
        ais.close();
        tmp.close();
    }
    xhtml.endDocument();
}
Also used : StreamingNotSupportedException(org.apache.commons.compress.archivers.StreamingNotSupportedException) TikaException(org.apache.tika.exception.TikaException) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) TikaConfig(org.apache.tika.config.TikaConfig) EmbeddedDocumentExtractor(org.apache.tika.extractor.EmbeddedDocumentExtractor) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) MediaTypeRegistry(org.apache.tika.mime.MediaTypeRegistry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) PasswordRequiredException(org.apache.commons.compress.PasswordRequiredException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) PasswordProvider(org.apache.tika.parser.PasswordProvider) UnsupportedZipFeatureException(org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) CpioArchiveInputStream(org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) DumpArchiveInputStream(org.apache.commons.compress.archivers.dump.DumpArchiveInputStream) SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile) BufferedInputStream(java.io.BufferedInputStream) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Aggregations

SevenZFile (org.apache.commons.compress.archivers.sevenz.SevenZFile)3 BufferedInputStream (java.io.BufferedInputStream)2 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)2 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)2 SevenZArchiveEntry (org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 PasswordRequiredException (org.apache.commons.compress.PasswordRequiredException)1 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)1 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)1 StreamingNotSupportedException (org.apache.commons.compress.archivers.StreamingNotSupportedException)1 ArArchiveInputStream (org.apache.commons.compress.archivers.ar.ArArchiveInputStream)1 CpioArchiveInputStream (org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream)1 DumpArchiveInputStream (org.apache.commons.compress.archivers.dump.DumpArchiveInputStream)1 JarArchiveInputStream (org.apache.commons.compress.archivers.jar.JarArchiveInputStream)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 UnsupportedZipFeatureException (org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1