Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry isDirectory

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry isDirectory

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Is this entry a directory?

Usage

From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java

@Test
public void shouldUnpackContentsOfZipSources() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp);
    workspace.setUp();//from www .  ja  v a2  s .  c  o m

    Path zip = workspace.buildAndReturnOutput("//example:zipsources");

    try (ZipFile zipFile = new ZipFile(zip.toFile())) {
        ZipArchiveEntry menu = zipFile.getEntry("menu.txt");
        assertThat(menu, Matchers.notNullValue());
        assertFalse(menu.isUnixSymlink());
        assertFalse(menu.isDirectory());
        ZipArchiveEntry cake = zipFile.getEntry("cake.txt");
        assertThat(cake, Matchers.notNullValue());
        assertFalse(cake.isUnixSymlink());
        assertFalse(cake.isDirectory());
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * Top unzip method. extract tarfile to constituent parts processing gzips 
 * along the way e.g. yyyyMMdd.zip->/yyyyMMdd/INode-CH_RNC01/A2010...zip
 *//*from   w w  w  . jav  a  2  s.  co m*/
protected void unzip1(File zipfile) throws FileNotFoundException {

    try {
        ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile));
        ZipArchiveEntry z1 = null;
        while ((z1 = zais.getNextZipEntry()) != null) {
            if (z1.isDirectory()) {
                /*hack to add vcc identifier because fucking ops cant rename a simple file*/
                if (z1.getName().contains("account"))
                    identifier = ".vcc";
                else
                    identifier = "";
            } else {
                String fn = z1.getName().substring(z1.getName().lastIndexOf("/"));
                File f = new File(getCalTempPath() + fn);
                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

                int n = 0;
                byte[] content = new byte[BUFFER];
                while (-1 != (n = zais.read(content))) {
                    fos.write(content, 0, n);
                }

                bos.flush();
                bos.close();
                fos.close();

                File unz = null;
                if (f.getName().endsWith("zip"))
                    unz = unzip3(f);
                else
                    unz = ungzip(f);

                if (unz != null)
                    allfiles.add(unz);
                f.delete();
            }
        }
        zais.close();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:com.facebook.buck.features.zip.rules.ZipRuleIntegrationTest.java

@Test
public void shouldZipSources() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp);
    workspace.setUp();//from  www .ja  v  a 2 s .co m

    Path zip = workspace.buildAndReturnOutput("//example:ziptastic");

    // Make sure we have the right files and attributes.
    try (ZipFile zipFile = new ZipFile(zip.toFile())) {
        ZipArchiveEntry cake = zipFile.getEntry("cake.txt");
        assertThat(cake, Matchers.notNullValue());
        assertFalse(cake.isUnixSymlink());
        assertFalse(cake.isDirectory());

        ZipArchiveEntry beans = zipFile.getEntry("beans/");
        assertThat(beans, Matchers.notNullValue());
        assertFalse(beans.isUnixSymlink());
        assertTrue(beans.isDirectory());

        ZipArchiveEntry cheesy = zipFile.getEntry("beans/cheesy.txt");
        assertThat(cheesy, Matchers.notNullValue());
        assertFalse(cheesy.isUnixSymlink());
        assertFalse(cheesy.isDirectory());
    }
}

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

public void expand(File source, File destDir) throws IOException {
    assertIsZip(source);/*from   w  ww.j  ava  2 s.  com*/

    try (ZipFile zf = new ZipFile(source)) {
        byte[] b = new byte[512];

        Enumeration entries = zf.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry e = (ZipArchiveEntry) entries.nextElement();
            //log.debug("Extracting zip: "+ficheroZip.getName());

            // baskslash-protection: zip format expects only 'fw' slashes
            String name = FileUtils.toCanonicalPath(e.getName());

            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 zip: had to create parent: "+outFile.getParentFile());
                outFile.getParentFile().mkdirs();
            }
            try (FileOutputStream fos = new FileOutputStream(outFile); InputStream is = zf.getInputStream(e)) {
                int len;
                while ((len = is.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
            }
        }
    }
}

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

public boolean extractOne(File source, String path, File dest) throws IOException {
    assertIsZip(source);/*w ww.  j  a v a2 s .  co m*/

    try (ZipFile zf = new ZipFile(source)) {
        byte[] b = new byte[512];

        //log.debug("Extracting zip: "+ficheroZip.getName());
        Enumeration entries = zf.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry e = (ZipArchiveEntry) entries.nextElement();

            // baskslash-protection: zip format expects only 'fw' slashes
            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 zip: had to create parent: "+outFile.getParentFile());
                dest.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(dest); InputStream is = zf.getInputStream(e)) {
                int len;
                while ((len = is.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                return true;
            }
        }
    }
    return false;
}

From source file:com.github.wolfdogs.kemono.util.resource.zip.ZipDirResource.java

private void createResource(String path, ZipArchiveEntry entry) {
    String[] childs = StringUtils.split(path, "/\\");

    ZipDirResource res = this;
    for (int i = 0; i < childs.length - 1; i++) {
        String name = childs[i];// w ww.  j  a va  2s . com

        ZipDirResource child = (ZipDirResource) res.getDir(name);
        if (child == null)
            child = new ZipDirResource(res, zipFile);

        res.addResource(name, child);
        res = child;
    }

    if (entry.isDirectory()) {
        ZipDirResource child = new ZipDirResource(res, zipFile);
        child.setEntry(entry);
        res.addResource(childs[childs.length - 1], child);
    } else {
        ZipFileResource child = new ZipFileResource(res, zipFile, entry);
        res.addResource(childs[childs.length - 1], child);
    }
}

From source file:brut.directory.ZipRODirectory.java

private void loadAll() {
    mFiles = new LinkedHashSet<String>();
    mDirs = new LinkedHashMap<String, AbstractDirectory>();

    int prefixLen = getPath().length();
    Enumeration<? extends ZipArchiveEntry> entries = getZipFile().getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();

        // ignore general purpose bit, since AOSP does
        entry.getGeneralPurposeBit().useEncryption(false);
        String name = entry.getName();

        if (name.equals(getPath()) || !name.startsWith(getPath())) {
            continue;
        }/*w ww  .j  a va 2 s. co  m*/

        String subname = name.substring(prefixLen);

        int pos = subname.indexOf(separator);
        if (pos == -1) {
            if (!entry.isDirectory()) {
                mFiles.add(subname);
                continue;
            }
        } else {
            subname = subname.substring(0, pos);
        }

        if (!mDirs.containsKey(subname)) {
            AbstractDirectory dir = new ZipRODirectory(getZipFile(), getPath() + subname + separator);
            mDirs.put(subname, dir);
        }
    }
}

From source file:com.google.cloud.tools.managedcloudsdk.install.ZipExtractorProvider.java

@Override
public void extract(Path archive, Path destination, ProgressListener progressListener) throws IOException {

    progressListener.start("Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN);

    String canonicalDestination = destination.toFile().getCanonicalPath();

    // Use ZipFile instead of ZipArchiveInputStream so that we can obtain file permissions
    // on unix-like systems via getUnixMode(). ZipArchiveInputStream doesn't have access to
    // all the zip file data and will return "0" for any call to getUnixMode().
    try (ZipFile zipFile = new ZipFile(archive.toFile())) {
        // TextProgressBar progressBar = textBarFactory.newProgressBar(messageListener, count);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            Path entryTarget = destination.resolve(entry.getName());

            String canonicalTarget = entryTarget.toFile().getCanonicalPath();
            if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) {
                throw new IOException("Blocked unzipping files outside destination: " + entry.getName());
            }/*from w w w.  ja v  a2  s.  com*/

            progressListener.update(1);
            logger.fine(entryTarget.toString());

            if (entry.isDirectory()) {
                if (!Files.exists(entryTarget)) {
                    Files.createDirectories(entryTarget);
                }
            } else {
                if (!Files.exists(entryTarget.getParent())) {
                    Files.createDirectories(entryTarget.getParent());
                }
                try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
                    try (InputStream in = zipFile.getInputStream(entry)) {
                        IOUtils.copy(in, out);
                        PosixFileAttributeView attributeView = Files.getFileAttributeView(entryTarget,
                                PosixFileAttributeView.class);
                        if (attributeView != null) {
                            attributeView
                                    .setPermissions(PosixUtil.getPosixFilePermissions(entry.getUnixMode()));
                        }
                    }
                }
            }
        }
    }
    progressListener.done();
}

From source file:at.spardat.xma.xdelta.JarDelta.java

/**
 * Find best source./*from www. ja  va  2  s  .  com*/
 *
 * @param source the source
 * @param target the target
 * @param targetEntry the target entry
 * @return the zip archive entry
 * @throws IOException Signals that an I/O exception has occurred.
 */
public ZipArchiveEntry findBestSource(ZipFile source, ZipFile target, ZipArchiveEntry targetEntry)
        throws IOException {
    ArrayList<ZipArchiveEntry> ret = new ArrayList<>();
    for (ZipArchiveEntry next : source.getEntries(targetEntry.getName())) {
        if (next.getCrc() == targetEntry.getCrc())
            return next;
        ret.add(next);
    }
    if (ret.size() == 0)
        return null;
    if (ret.size() == 1 || targetEntry.isDirectory())
        return ret.get(0);
    //More than one and no matching crc --- need to calculate xdeltas and pick the  shortest
    ZipArchiveEntry retEntry = null;
    for (ZipArchiveEntry sourceEntry : ret) {
        try (ByteArrayOutputStream outbytes = new ByteArrayOutputStream()) {
            Delta d = new Delta();
            DiffWriter diffWriter = new GDiffWriter(new DataOutputStream(outbytes));
            int sourceSize = (int) sourceEntry.getSize();
            byte[] sourceBytes = new byte[sourceSize];
            try (InputStream sourceStream = source.getInputStream(sourceEntry)) {
                for (int erg = sourceStream.read(sourceBytes); erg < sourceBytes.length; erg += sourceStream
                        .read(sourceBytes, erg, sourceBytes.length - erg))
                    ;
            }
            d.compute(sourceBytes, target.getInputStream(targetEntry), diffWriter);
            byte[] nextDiff = outbytes.toByteArray();
            if (calculatedDelta == null || calculatedDelta.length > nextDiff.length) {
                retEntry = sourceEntry;
                calculatedDelta = nextDiff;
            }
        }
    }
    return retEntry;
}

From source file:com.naryx.tagfusion.expression.function.file.ZipList.java

private cfQueryResultData performZiplist(cfSession session, File zipfile, String charset) throws IOException {
    ZipFile zFile = null;/*from   www . jav a2 s .  c o m*/
    try {
        cfQueryResultData filesQuery = new cfQueryResultData(new String[] { "name", "type", "compressedsize",
                "size", "compressedpercent", "datelastmodified", "comment" }, "CFZIP");
        zFile = new ZipFile(zipfile, charset);

        List<Map<String, cfData>> allResultRows = new ArrayList<Map<String, cfData>>();
        Map<String, cfData> resultRow;
        Enumeration<? extends ZipArchiveEntry> files = zFile.getEntries();
        ZipArchiveEntry nextEntry = null;
        long size;
        double compressed;

        while (files.hasMoreElements()) {
            nextEntry = (ZipArchiveEntry) files.nextElement();
            resultRow = new FastMap<String, cfData>(8);
            resultRow.put("name", new cfStringData(nextEntry.getName()));
            resultRow.put("comment", new cfStringData(nextEntry.getComment()));
            resultRow.put("datelastmodified", new cfDateData(nextEntry.getTime()));

            if (nextEntry.isDirectory()) {
                resultRow.put("compressedsize", new cfNumberData(0));
                resultRow.put("size", new cfNumberData(0));
                resultRow.put("type", new cfStringData("Dir"));
                resultRow.put("compressedpercent", new cfNumberData(0));

            } else {
                size = nextEntry.getSize();
                resultRow.put("compressedsize",
                        new cfStringData(String.valueOf(nextEntry.getCompressedSize())));
                resultRow.put("size", new cfStringData(String.valueOf(size)));
                resultRow.put("type", new cfStringData("File"));
                if (size != 0) {
                    compressed = ((float) nextEntry.getCompressedSize() / (float) size);
                    resultRow.put("compressedpercent",
                            new cfStringData(String.valueOf(100 - (int) (compressed * 100))));
                } else {
                    resultRow.put("compressedpercent", new cfStringData("0"));
                }
            }

            allResultRows.add(resultRow);
        }
        filesQuery.populateQuery(allResultRows);
        return filesQuery;
    } finally {
        try {
            zFile.close();
        } catch (IOException ignored) {
        }
    }
}