Example usage for org.apache.commons.compress.archivers.zip ZipFile getEntriesInPhysicalOrder

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getEntriesInPhysicalOrder

Introduction

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

Prototype

public Enumeration getEntriesInPhysicalOrder() 

Source Link

Document

Returns all entries in physical order.

Usage

From source file:org.artificer.atom.archive.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file/*from w  w  w .j  a  v a  2 s  .  c  om*/
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntriesInPhysicalOrder();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(Messages.i18n.format("FAILED_TO_CREATE_PARENT_DIR",
                            outFile.getParentFile().getCanonicalPath()));
                }
            }

            if (entry.isDirectory()) {
                if (outFile.isDirectory()) {
                    // Do nothing - already created.
                } else if (outFile.isFile()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath()));
                } else if (!outFile.mkdir()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath()));
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java

@SuppressWarnings("OctalInteger")
public void testBasic() throws Exception {
    File zip1 = testUtil.getTestFile("src/test/resources/zip/zip-1");
    File zip = testUtil.getTestFile("target/zip/zip-1/test.zip");
    if (!zip.getParentFile().isDirectory()) {
        assertTrue(zip.getParentFile().mkdirs());
    }//from  www.ja va  2s .  c om

    LocalFs basedir = new LocalFs(zip1);

    ZipUnixPackage zipPackage = new ZipUnixPackage(new SystemStreamLog());

    zipPackage.beforeAssembly(EMPTY.mode(UnixFileMode._0755), timestamp);

    assertTrue(basedir.isDirectory());

    // Git set the timestamp of file objects
    assertTrue(new File(zip1, "dirs").setLastModified(dirsTimestamp.toDateTime().getMillis()));
    assertTrue(new File(zip1, "dirs/bar.txt").setLastModified(dirsBarTxtTimestamp.toDateTime().getMillis()));
    assertTrue(new File(zip1, "file").setLastModified(fileTimestamp.toDateTime().getMillis()));
    assertTrue(new File(zip1, "file/foo.txt").setLastModified(fileFooTxtTimestamp.toDateTime().getMillis()));

    Replacer replacer = new Replacer("@bar@", "awesome");

    new CreateDirectoriesOperation(timestamp, new String[] { "/opt/hudson" }, EMPTY).perform(zipPackage);

    new CopyDirectoryOperation(basedir, relativePath(""), List.<String>nil(), List.<String>nil(),
            Option.<P2<String, String>>none(), EMPTY, EMPTY).perform(zipPackage);

    new CopyFileOperation(EMPTY, basedir.resolve("file/foo.txt"), relativePath("/file/foo.txt"))
            .perform(zipPackage);

    new SymlinkOperation(relativePath("/var/log/hudson"), "/var/opt/hudson/log", Option.<String>none(),
            Option.<String>none()).perform(zipPackage);

    new FilterFilesOperation(single("dirs/**"), List.<String>nil(), single(replacer), LineEnding.unix)
            .perform(zipPackage);

    UnixFileMode fileMode = UnixFileMode.fromInt(0600);
    UnixFileMode dirMode = _0777;

    FileAttributes fileAttributes = new FileAttributes("root", "root", fileMode);
    FileAttributes directoryAttributes = new FileAttributes("root", "root", dirMode);
    new SetAttributesOperation(BASE, single("**/*"), List.<String>nil(), some(fileAttributes),
            some(directoryAttributes)).perform(zipPackage);

    zipPackage.prepare(SINGLE).packageToFile(zip);

    ZipFile file = new ZipFile(zip);
    Enumeration<ZipArchiveEntry> enumeration = file.getEntriesInPhysicalOrder();
    assertDirectory(enumeration.nextElement(), "./dirs/", dirsTimestamp);
    // Is it really correct that filtered files should retain the old timestamp?
    assertFile(file, enumeration.nextElement(), "./dirs/bar.txt", 8, dirsBarTxtTimestamp, "awesome\n",
            fileMode);
    assertDirectory(enumeration.nextElement(), "./file/", fileTimestamp);
    assertFile(file, enumeration.nextElement(), "./file/foo.txt", 6, fileFooTxtTimestamp, "@foo@\n", fileMode);
    assertDirectory(enumeration.nextElement(), "./opt/", timestamp);
    assertDirectory(enumeration.nextElement(), "./opt/hudson/", timestamp);
    assertFalse(enumeration.hasMoreElements());
    file.close();
}

From source file:org.codehaus.plexus.archiver.zip.PlexusIoZipFileResourceCollection.java

protected Iterator<PlexusIoResource> getEntries() throws IOException {
    final File f = getFile();
    if (f == null) {
        throw new IOException("The zip file has not been set.");
    }//w ww  .  j  ava 2 s.c om
    final URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { f.toURI().toURL() }, null) {
        public URL getResource(String name) {
            return findResource(name);
        }
    };

    final URL url = new URL("jar:" + f.toURI().toURL() + "!/");
    final ZipFile zipFile = new ZipFile(f, charset != null ? charset.name() : "UTF8");
    final Enumeration<ZipArchiveEntry> en = zipFile.getEntriesInPhysicalOrder();
    return new ZipFileResourceIterator(en, url, zipFile, urlClassLoader);
}

From source file:org.overlord.sramp.atom.archive.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file//www .  j  ava2 s  .  c om
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntriesInPhysicalOrder();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(Messages.i18n.format("FAILED_TO_CREATE_PARENT_DIR", //$NON-NLS-1$
                            outFile.getParentFile().getCanonicalPath()));
                }
            }

            if (entry.isDirectory()) {
                if (!outFile.mkdir()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath())); //$NON-NLS-1$
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}