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

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

Introduction

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

Prototype

public ZipFile(String name) throws IOException 

Source Link

Document

Opens the given file for reading, assuming "UTF8".

Usage

From source file:at.spardat.xma.xdelta.test.JarDeltaJarPatcherTest.java

/**
 * Test embedded zip.//w  w w.  j a v a2 s  .c  o m
 *
 * @throws Exception the exception
 */
@Test
public void testEmbeddedZip() throws Exception {
    String file1 = "src/test/resources/embedded1.zip";
    String file2 = "src/test/resources/embedded2.zip";
    runJarPatcher(file1, file2, new ZipFile(file1), new ZipFile(file2), false);
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

/**
 * zip?/*from ww w  .ja va 2  s . co m*/
 *
 * @param zipFile
 * @param path
 * @param destFolder
 * @throws java.io.IOException
 */
public static File extractZipFileToFolder(File zipFile, String path, File destFolder) {
    ZipFile zip;
    File destFile = null;
    try {
        zip = new ZipFile(zipFile);
        ZipArchiveEntry zipArchiveEntry = zip.getEntry(path);
        if (null != zipArchiveEntry) {
            String name = zipArchiveEntry.getName();
            name = FilenameUtils.getName(name);
            destFile = new File(destFolder, name);
            FileMkUtils.mkdirs(destFolder);
            destFile.createNewFile();
            InputStream is = zip.getInputStream(zipArchiveEntry);
            FileOutputStream fos = new FileOutputStream(destFile);
            int length = 0;
            byte[] b = new byte[1024];
            while ((length = is.read(b, 0, 1024)) != -1) {
                fos.write(b, 0, length);
            }
            is.close();
            fos.close();
        }
        if (null != zip) {
            ZipFile.closeQuietly(zip);
        }
    } catch (IOException e) {
        throw new GradleException(e.getMessage(), e);
    }
    return destFile;
}

From source file:com.facebook.buck.util.ProjectFilesystemTest.java

@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {

    // Create a empty executable file.
    File exe = tmp.newFile("test.exe");
    exe.setExecutable(true);//from  w w  w  .  j a v  a  2  s  .  c  om

    // Archive it into a zipfile using `ProjectFileSystem.createZip`.
    File zipFile = new File(tmp.getRoot().toPath().toString() + "/test.zip");
    filesystem.createZip(ImmutableList.of(exe.toPath()), zipFile);

    // Now unpack the archive (using apache's common-compress, as it preserves
    // executable permissions) and verify that the archive entry has executable
    // permissions.
    try (ZipFile zip = new ZipFile(zipFile)) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        assertTrue(entries.hasMoreElements());
        ZipArchiveEntry entry = entries.nextElement();
        Set<PosixFilePermission> permissions = MorePosixFilePermissions
                .fromMode(entry.getExternalAttributes() >> 16);
        assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
        assertFalse(entries.hasMoreElements());
    }

}

From source file:fr.acxio.tools.agia.tasks.ZipFilesTaskletTest.java

@Test
public void testExistingZipFile() throws Exception {
    File aTargetFile = File.createTempFile("target/Z8-input", ".zip");
    String aTargetFilename = aTargetFile.getAbsolutePath();

    ZipFilesTasklet aTasklet = new ZipFilesTasklet();
    aTasklet.setSourceBaseDirectory(new FileSystemResource("src/test/resources/testFiles/"));
    FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
    aSourceFactory.setPattern("file:src/test/resources/testFiles/input.csv");
    aTasklet.setSourceFactory(aSourceFactory);
    ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
    aDestinationFactory.setExpression(aTargetFilename);
    aTasklet.setDestinationFactory(aDestinationFactory);

    assertTrue(new File(aTargetFilename).exists());

    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
    verify(aStepContribution, times(1)).incrementReadCount();
    verify(aStepContribution, times(1)).incrementWriteCount(1);

    assertTrue(new File(aTargetFilename).exists());
    ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
    Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
    assertTrue(aEntries.hasMoreElements());
    assertEquals("input.csv", aEntries.nextElement().getName());
    assertFalse(aEntries.hasMoreElements());
    aZipFile.close();/* w ww. ja  v  a 2 s  .  c o  m*/
}

From source file:abfab3d.io.input.ModelLoader.java

/**
 * Unzip a file into a destination directory
 *
 * @param src//  w  ww  .  ja v a2  s  .c o  m
 * @param dest
 */
private static void unzip(File src, File dest) throws IOException {
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(src);

        for (Enumeration e = zipFile.getEntries(); e.hasMoreElements();) {
            ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
            unzipEntry(zipFile, entry, dest);
        }
    } finally {
        if (zipFile != null)
            zipFile.close();
    }
}

From source file:at.spardat.xma.xdelta.test.JarDeltaJarPatcherTest.java

/**
 * Tests JarDelta and JarPatcher on two identical files.
 *
 * @throws Exception the exception/*from  www  .  j ava 2  s.  c  o  m*/
 */
@Test
public void testJarPatcherIdentFile() throws Exception {
    ZipFile originalZip = makeSourceZipFile(sourceFile);
    new JarDelta().computeDelta(sourceFile.getAbsolutePath(), sourceFile.getAbsolutePath(), originalZip,
            originalZip, new ZipArchiveOutputStream(new FileOutputStream(patchFile)));
    ZipFile patch = new ZipFile(patchFile);
    ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list");
    if (listEntry == null) {
        patch.close();
        throw new IOException("Invalid patch - list entry 'META-INF/file.list' not found");
    }
    BufferedReader patchlist = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry)));
    String next = patchlist.readLine();
    String sourceName = next;
    next = patchlist.readLine();
    ZipFile source = new ZipFile(sourceFile);
    new JarPatcher(patchFile.getName(), sourceName).applyDelta(patch, source,
            new ZipArchiveOutputStream(new FileOutputStream(resultFile)), patchlist);
    compareFiles(new ZipFile(sourceFile), new ZipFile(resultFile));
}

From source file:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java

@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {

    // Create a empty executable file.
    Path exe = tmp.newFile("test.exe");
    MostFiles.makeExecutable(exe);//w w w . j av a2 s .co  m

    // Archive it into a zipfile using `Zip.create`.
    Path zipFile = tmp.getRoot().resolve("test.zip");
    Zip.create(filesystem, ImmutableList.of(exe), zipFile);

    // Now unpack the archive (using apache's common-compress, as it preserves
    // executable permissions) and verify that the archive entry has executable
    // permissions.
    try (ZipFile zip = new ZipFile(zipFile.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        assertTrue(entries.hasMoreElements());
        ZipArchiveEntry entry = entries.nextElement();
        Set<PosixFilePermission> permissions = MorePosixFilePermissions
                .fromMode(entry.getExternalAttributes() >> 16);
        assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
        assertFalse(entries.hasMoreElements());
    }
}

From source file:fr.acxio.tools.agia.tasks.ZipFilesTaskletTest.java

@Test
public void testExistingLockedZipFile() throws Exception {
    exception.expect(IOException.class);
    File aTargetFile = File.createTempFile("target/Z9-input", ".zip");
    aTargetFile.setWritable(false);/*w w w  .  j  a va 2s  . c  o  m*/
    String aTargetFilename = aTargetFile.getAbsolutePath();

    ZipFilesTasklet aTasklet = new ZipFilesTasklet();
    aTasklet.setSourceBaseDirectory(new FileSystemResource("src/test/resources/testFiles/"));
    FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
    aSourceFactory.setPattern("file:src/test/resources/testFiles/input.csv");
    aTasklet.setSourceFactory(aSourceFactory);
    ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
    aDestinationFactory.setExpression(aTargetFilename);
    aTasklet.setDestinationFactory(aDestinationFactory);

    assertTrue(new File(aTargetFilename).exists());

    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
    verify(aStepContribution, times(1)).incrementReadCount();
    verify(aStepContribution, times(1)).incrementWriteCount(1);

    assertTrue(new File(aTargetFilename).exists());
    ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
    Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
    assertTrue(aEntries.hasMoreElements());
    assertEquals("input.csv", aEntries.nextElement().getName());
    assertFalse(aEntries.hasMoreElements());
    aZipFile.close();
}

From source file:adams.core.io.ZipUtils.java

/**
 * Lists the files stored in the ZIP file.
 *
 * @param input   the ZIP file to obtain the file list from
 * @param listDirs   whether to include directories in the list
 * @return      the stored files/*from ww  w. j a v  a2s  .c  o  m*/
 */
public static List<File> listFiles(File input, boolean listDirs) {
    List<File> result;
    ZipFile zipfile;
    Enumeration<ZipArchiveEntry> enm;
    ZipArchiveEntry entry;

    result = new ArrayList<>();
    zipfile = null;
    try {
        zipfile = new ZipFile(input.getAbsoluteFile());
        enm = zipfile.getEntries();
        while (enm.hasMoreElements()) {
            entry = enm.nextElement();

            // extract
            if (entry.isDirectory()) {
                if (listDirs)
                    result.add(new File(entry.getName()));
            } else {
                result.add(new File(entry.getName()));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (Exception e) {
                // ignored
            }
        }
    }

    return result;
}

From source file:com.taobao.android.utils.ZipUtils.java

public static List<String> listZipEntries(File zipFile) {
    List<String> list = new ArrayList<String>();
    ZipFile zip;//from   ww  w.j  a  v  a2 s  .  co m
    try {
        zip = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = zip.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            String name = ze.getName();
            list.add(name);
        }
        if (null != zip)
            ZipFile.closeQuietly(zip);
    } catch (IOException e) {
    }
    return list;
}