Example usage for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry isDirectory

List of usage examples for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry isDirectory

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry isDirectory.

Prototype

boolean isDirectory

To view the source code for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry isDirectory.

Click Source Link

Usage

From source file:at.treedb.util.SevenZip.java

/**
 * Extracts some data (files and directories) from the archive without
 * extracting the whole archive./*from  ww w .j a  va2s. co m*/
 * 
 * @param archive
 *            7-zip archive
 * @param fileList
 *            file extraction list, a path with an ending '/' denotes a
 *            directory
 * @return file list as a map file name/file data
 * @throws IOException
 */
public static HashMap<String, byte[]> exctact(File archive, String... fileList) throws IOException {
    HashSet<String> fileSet = new HashSet<String>();
    ArrayList<String> dirList = new ArrayList<String>();
    for (String f : fileList) {
        if (!f.endsWith("/")) {
            fileSet.add(f);
        } else {
            dirList.add(f);
        }
    }
    HashMap<String, byte[]> resultMap = new HashMap<String, byte[]>();
    SevenZFile sevenZFile = new SevenZFile(archive);
    do {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        // convert window path to unix style
        String name = entry.getName().replace('\\', '/');
        if (!entry.isDirectory()) {
            boolean storeFile = false;
            if (fileSet.contains(name)) {
                storeFile = true;
            } else {
                // search directories
                for (String s : dirList) {
                    if (name.startsWith(s)) {
                        storeFile = true;
                        break;
                    }
                }
            }
            // store the file
            if (storeFile) {
                int size = (int) entry.getSize();
                byte[] data = new byte[size];
                sevenZFile.read(data, 0, size);
                resultMap.put(name, data);
                // in this case we can finish the extraction loop
                if (dirList.isEmpty() && resultMap.size() == fileSet.size()) {
                    break;
                }
            }
        }
    } while (true);
    sevenZFile.close();
    return resultMap;
}

From source file:com.github.n_i_e.dirtreedb.SevenZipListerForFile.java

@Override
protected PathEntry getNext() throws IOException {
    SevenZArchiveEntry z = sevenzfile.getNextEntry();
    if (z == null) {
        return null;
    }/*from ww w .j  a v  a  2 s . c  o m*/
    int newtype = z.isDirectory() ? PathEntry.COMPRESSEDFOLDER : PathEntry.COMPRESSEDFILE;
    String s = z.getName();
    if (s == null) {
        s = AbstractCompressorLister.getBasename(getBasePath());
    }
    ;
    s = s.replace("\\", "/");
    if (z.isDirectory() && !s.endsWith("/")) {
        s = s + "/";
    }
    PathEntry next_entry = new PathEntry(getBasePath().getPath() + "/" + s, newtype);
    next_entry.setDateLastModified(z.getLastModifiedDate().getTime());
    next_entry.setStatus(PathEntry.DIRTY);
    next_entry.setSize(z.getSize());
    next_entry.setCompressedSize(z.getSize());
    if (isCsumRequested() && newtype == PathEntry.COMPRESSEDFILE) {
        next_entry.setCsum(new SevenZipInputStream());
    }
    if (next_entry.getSize() < 0) {
        next_entry.setSize(0);
    }
    if (next_entry.getCompressedSize() < 0) {
        next_entry.setCompressedSize(next_entry.getSize());
    }
    return next_entry;
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public ArrayList<String> list(File source) throws IOException {
    assertIs7Zip(source);/*  ww  w  .j  a v  a  2  s .  c  o  m*/

    SevenZFile zf = new SevenZFile(source);
    ArrayList<String> paths = new ArrayList<String>();

    for (SevenZArchiveEntry e : zf.getEntries()) {
        String name = FileUtils.toCanonicalPath(e.getName());
        if (e.isDirectory()) {
            continue;
        }

        paths.add(name);
    }
    return paths;
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public boolean extractOne(File source, String path, File dest) throws IOException {
    assertIs7Zip(source);/*from  ww  w.ja  v  a 2 s  . c o m*/

    try (SevenZFile zf = new SevenZFile(source)) {
        for (SevenZArchiveEntry e : zf.getEntries()) {
            String name = FileUtils.toCanonicalPath(e.getName());
            // System.err.println(" "+name+" =? "+path);
            if (!name.equals(path) || e.isDirectory()) {
                continue;
            }

            if (!dest.getParentFile().exists()) {
                //log.warn("weird 7z: had to create parent: "+outFile.getParentFile());
                dest.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(dest)) {
                byte[] b = new byte[(int) e.getSize()];
                zf.read(b);
                return true;
            }
        }
    }
    return false;
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public void expand(File source, File destDir) throws IOException {
    assertIs7Zip(source);//from  ww w .j  a  v  a 2s.c  om

    try (SevenZFile zf = new SevenZFile(source)) {
        log.info("Extracting 7zip: " + source.getName());

        while (true) {
            SevenZArchiveEntry e = zf.getNextEntry();
            if (e == null)
                return;

            String name = FileUtils.toCanonicalPath(e.getName());
            log.info(" - processing 7zip entry: " + name + " - " + e.getSize());

            if (e.isDirectory()) {
                //log.debug("\tExtracting directory "+e.getName());
                File dir = new File(destDir, name);
                dir.mkdirs();
                continue;
            }

            //log.debug("\tExtracting file "+name);
            File outFile = new File(destDir, name);
            if (!outFile.getParentFile().exists()) {
                //log.warn("weird 7z: had to create parent: "+outFile.getParentFile());
                outFile.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(outFile)) {
                byte[] b = new byte[(int) e.getSize()];
                zf.read(b);
                fos.write(b);
            }
        }
    }
}

From source file:at.treedb.util.SevenZip.java

/**
 * Constructor//from   www .j a  va2 s .  c  om
 * 
 * @param file
 *            7-zip archive file
 * @param iface
 *            interface to display the extraction progress
 * @throws IOException
 */
public SevenZip(File file, DomainInstallationIface iface) throws Exception {
    sevenZFile = new SevenZFile(file);
    entryMap = new HashMap<String, ArchiveEntry>();
    // create a temporary file containing the extracted archive
    dumpFile = FileStorage.getInstance().createTempFile("sevenZip", ".dump");
    FileOutputStream writer = new FileOutputStream(dumpFile);
    byte[] readBuffer = new byte[READ_BUFFER];
    int offset = 0;
    if (iface != null) {
        iface.writeInternalMessage(InstallationProgress.Type.MSG,
                "Extracting installation package: '.' stands for " + READ_BUFFER + " MB data");
        iface.writeInternalMessage(null, "<br>");
    }
    int count = 0;
    int bytesWritten = 0;
    // extract and dump the files
    do {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (!entry.isDirectory()) {
            int readBytes = (int) entry.getSize();
            int size = readBytes;
            int index = 0;
            while (readBytes > 0) {
                int read = Math.min(readBytes, READ_BUFFER);
                bytesWritten += read;
                if (iface != null) {
                    iface.writeInternalMessage(null, "<b>.</b>");
                    ++count;
                    if (count == 80) {
                        iface.writeInternalMessage(null, "<br>");
                        count = 0;
                    }
                }
                sevenZFile.read(readBuffer, index, read);
                writer.write(readBuffer, 0, read);
                readBytes -= read;
            }
            entryMap.put(entry.getName().replace('\\', '/'), new ArchiveEntry(offset, size));
            offset += size;
        }
    } while (true);
    if (iface != null) {
        iface.writeInternalMessage(null, "<br>");
        iface.writeInternalMessage(InstallationProgress.Type.MSG,
                "Extraction finished: " + bytesWritten + " bytes written");
    }
    writer.close();
    dumpReader = new FileInputStream(dumpFile);
    channel = dumpReader.getChannel();
}

From source file:at.treedb.backup.Import.java

/**
 * Read all archive entries./*from   w ww . j  a v a 2 s.c o m*/
 * 
 * @return archive entries
 * @throws IOException
 */
private HashMap<String, SevenZArchiveEntry> readEntries() throws IOException {
    HashMap<String, SevenZArchiveEntry> archiveMap = new HashMap<String, SevenZArchiveEntry>();
    sevenZFile = new SevenZFile(new File(archivePath));
    while (true) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (entry.isDirectory()) {
            continue;
        }
        archiveMap.put(entry.getName(), entry);
    }
    sevenZFile.close();
    return archiveMap;
}

From source file:at.treedb.backup.Import.java

/**
 * Reads a archive entry.// w ww.ja  v a 2 s  .  co  m
 * 
 * @param path
 *            archive path
 * @return archive entry data
 * @throws IOException
 */
private byte[] readData(String path) throws IOException {
    if (!archiveMap.containsKey(path)) {
        return null;
    }
    byte[] content = null;
    sevenZFile = new SevenZFile(new File(archivePath));
    while (true) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (entry.isDirectory()) {
            continue;
        }
        if (entry.getName().equals(path)) {
            int size = (int) entry.getSize();
            content = new byte[size];
            int index = 0;
            while (size > 0) {
                int read = sevenZFile.read(content, index, size);
                size -= read;
                index += read;
            }
            break;
        }
    }
    sevenZFile.close();
    return content;
}

From source file:at.treedb.backup.Import.java

public void writeFile(DAOiface dao, DBfile file, String path) throws Exception {
    if (!archiveMap.containsKey(path)) {
        throw new Exception("Import.writeFile(): path not found - " + path);
    }/*w w  w.  ja va2  s .c  o m*/
    byte[] content = null;
    sevenZFile = new SevenZFile(new File(archivePath));
    while (true) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (entry.isDirectory()) {
            continue;
        }
        if (entry.getName().equals(path)) {
            int size = (int) entry.getSize();
            content = new byte[Math.min(Export.BUFFER_SIZE, size)];
            int read = content.length;
            DBoutputStream os = new DBoutputStream(file, dao);
            while (size > 0) {
                int readCount = sevenZFile.read(content, 0, read);
                size -= readCount;
                read = Math.min(size, read);
                os.write(content, 0, readCount);
            }
            os.close();
            break;
        }
    }
    sevenZFile.close();
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.internal.actions.Explode.java

private void extract7z(ActionDescription aAction, Path aCachedFile, Path aTarget)
        throws IOException, RarException {
    // We always extract archives into a subfolder. Figure out the name of the folder.
    String base = getBase(aCachedFile.getFileName().toString());

    Map<String, Object> cfg = aAction.getConfiguration();
    int strip = cfg.containsKey("strip") ? (int) cfg.get("strip") : 0;

    AntFileFilter filter = new AntFileFilter(coerceToList(cfg.get("includes")),
            coerceToList(cfg.get("excludes")));

    try (SevenZFile archive = new SevenZFile(aCachedFile.toFile())) {
        SevenZArchiveEntry entry = archive.getNextEntry();
        while (entry != null) {
            String name = stripLeadingFolders(entry.getName(), strip);

            if (name == null) {
                // Stripped to null - nothing left to extract - continue;
                continue;
            }/*  w  ww. j a  va2 s.  c o  m*/

            if (filter.accept(name)) {
                Path out = aTarget.resolve(base).resolve(name);
                if (entry.isDirectory()) {
                    Files.createDirectories(out);
                } else {
                    Files.createDirectories(out.getParent());
                    try (OutputStream os = Files.newOutputStream(out)) {
                        InputStream is = new SevenZEntryInputStream(archive, entry);
                        IOUtils.copyLarge(is, os);
                    }
                }
            }

            entry = archive.getNextEntry();
        }
    }
}