Search in sources :

Example 11 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.

the class ZipRuleIntegrationTest method shouldZipSources.

@Test
public void shouldZipSources() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp);
    workspace.setUp();
    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());
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Test(org.junit.Test)

Example 12 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.

the class ZipStepTest method willRecurseIntoSubdirectories.

@Test
public void willRecurseIntoSubdirectories() throws IOException {
    Path parent = tmp.newFolder("zipstep");
    Path out = parent.resolve("output.zip");
    Path toZip = tmp.newFolder("zipdir");
    Files.createFile(toZip.resolve("file1.txt"));
    Files.createDirectories(toZip.resolve("child"));
    Files.createFile(toZip.resolve("child/file2.txt"));
    ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.DEFAULT_COMPRESSION_LEVEL, Paths.get("zipdir"));
    assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
    // Make sure we have the right attributes.
    try (ZipFile zip = new ZipFile(out.toFile())) {
        ZipArchiveEntry entry = zip.getEntry("child/");
        assertNotEquals(entry.getUnixMode() & MoreFiles.S_IFDIR, 0);
    }
    try (Zip zip = new Zip(out, false)) {
        assertEquals(ImmutableSet.of("file1.txt", "child/file2.txt"), zip.getFileNames());
    }
}
Also used : Path(java.nio.file.Path) Zip(com.facebook.buck.testutil.Zip) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Test(org.junit.Test)

Example 13 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.

the class ZipStepTest method minCompressionWritesCorrectZipFile.

/**
   * Tests a couple bugs:
   *     1) {@link com.facebook.buck.zip.OverwritingZipOutputStream} was writing uncompressed zip
   *        entries incorrectly.
   *     2) {@link ZipStep} wasn't setting the output size when writing uncompressed entries.
   */
@Test
public void minCompressionWritesCorrectZipFile() throws IOException {
    Path parent = tmp.newFolder("zipstep");
    Path out = parent.resolve("output.zip");
    Path toZip = tmp.newFolder("zipdir");
    byte[] contents = "hello world".getBytes();
    Files.write(toZip.resolve("file1.txt"), contents);
    Files.write(toZip.resolve("file2.txt"), contents);
    Files.write(toZip.resolve("file3.txt"), contents);
    ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.MIN_COMPRESSION_LEVEL, Paths.get("zipdir"));
    assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
    // directory and will verify it's valid.
    try (ZipFile zip = new ZipFile(out.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        ZipArchiveEntry entry1 = entries.nextElement();
        assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry1)));
        ZipArchiveEntry entry2 = entries.nextElement();
        assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry2)));
        ZipArchiveEntry entry3 = entries.nextElement();
        assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry3)));
    }
}
Also used : Path(java.nio.file.Path) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Test(org.junit.Test)

Example 14 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project ats-framework by Axway.

the class LocalFileSystemOperations method unzip.

/**
     * Unzip file to local or remote machine, if the machine is UNIX-like it will preserve the permissions
     *
     * @param zipFilePath the zip file path
     * @param outputDirPath output directory
     * @param isTempDirectory whether the directory is temporary or not. Temporary means that
     *  it will be automatically deleted only if the virtual machine terminates normally.
     * @throws Exception
     */
@Override
public void unzip(String zipFilePath, String outputDirPath) throws FileSystemOperationException {
    ZipArchiveEntry zipEntry = null;
    File outputDir = new File(outputDirPath);
    //check if the dir is created
    outputDir.mkdirs();
    try (ZipFile zipFile = new ZipFile(zipFilePath)) {
        Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
        int unixPermissions = 0;
        while (entries.hasMoreElements()) {
            zipEntry = entries.nextElement();
            File entryDestination = new File(outputDirPath, zipEntry.getName());
            unixPermissions = zipEntry.getUnixMode();
            if (zipEntry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                InputStream in = null;
                OutputStream out = null;
                in = zipFile.getInputStream(zipEntry);
                out = new BufferedOutputStream(new FileOutputStream(entryDestination));
                IoUtils.copyStream(in, out);
            }
            if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
                //check if the OS is UNIX
                // set file/dir permissions, after it is created
                Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(octalToDecimal(unixPermissions)));
            }
        }
    } catch (Exception e) {
        String errorMsg = "Unable to unzip " + ((zipEntry != null) ? zipEntry.getName() + " from " : "") + zipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
        throw new FileSystemOperationException(errorMsg, e);
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 15 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project zm-mailbox by Zimbra.

the class ZipUtil method getZipEntryNames.

/**
     * Traditional java.util.zip processing either assumes archives use UTF-8 for filenames or requires that
     * you know up front what charset is used for filenames.
     * This class uses the more versatile org.apache.commons.compress.archivers.zip package combined with
     * language information to make a best guess at what the filenames might be.
     *
     */
public static List<String> getZipEntryNames(InputStream inputStream, Locale locale) throws IOException {
    List<String> zipEntryNames = Lists.newArrayList();
    /*
         * From http://commons.apache.org/proper/commons-compress/zip.html
         * Traditionally the ZIP archive format uses CodePage 437 as encoding for file name, which is not sufficient for
         * many international character sets. Over time different archivers have chosen different ways to work around
         * the limitation - the java.util.zip packages simply uses UTF-8 as its encoding for example.
         *
         * For our purposes, CP437 has the advantage that all byte sequences are valid, so it works well as a final
         * fallback charset to assume for the name.
         */
    try (ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream, cp437charset.name(), false)) {
        ZipArchiveEntry ze;
        while ((ze = zis.getNextZipEntry()) != null) {
            if (ze.isDirectory()) {
                continue;
            }
            String entryName = bestGuessAtEntryName(ze, locale);
            zipEntryNames.add(entryName);
        }
    }
    return zipEntryNames;
}
Also used : ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Aggregations

ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)46 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)21 IOException (java.io.IOException)13 File (java.io.File)12 FileInputStream (java.io.FileInputStream)10 InputStream (java.io.InputStream)10 Path (java.nio.file.Path)10 Test (org.junit.Test)8 BufferedInputStream (java.io.BufferedInputStream)7 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)7 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)6 FileOutputStream (java.io.FileOutputStream)5 ArrayList (java.util.ArrayList)5 ZipInputStream (java.util.zip.ZipInputStream)5 ImageInfo (com.github.hmdev.info.ImageInfo)4 SectionInfo (com.github.hmdev.info.SectionInfo)4 BufferedWriter (java.io.BufferedWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileNotFoundException (java.io.FileNotFoundException)4