Search in sources :

Example 1 with SevenZArchiveEntry

use of org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry 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 SevenZArchiveEntry

use of org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry in project jphp by jphp-compiler.

the class PArchiveEntry method make7Zip.

protected SevenZArchiveEntry make7Zip(String name, ArrayMemory options) {
    SevenZArchiveEntry entry = new SevenZArchiveEntry();
    entry.setName(name);
    entry.setAntiItem(options.valueOfIndex("antiItem").toBoolean());
    entry.setDirectory(options.valueOfIndex("directory").toBoolean());
    if (options.containsKey("crc")) {
        entry.setCrcValue(options.valueOfIndex("crc").toLong());
        entry.setHasCrc(true);
    }
    if (options.containsKey("size")) {
        entry.setSize(options.valueOfIndex("size").toLong());
    }
    return entry;
}
Also used : SevenZArchiveEntry(org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry)

Aggregations

SevenZArchiveEntry (org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)1 SevenZFile (org.apache.commons.compress.archivers.sevenz.SevenZFile)1 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 Tx (org.structr.core.graph.Tx)1 Folder (org.structr.web.entity.Folder)1