Search in sources :

Example 1 with ZipInputStream

use of java.util.zip.ZipInputStream in project buck by facebook.

the class RepackZipEntriesStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) {
    Path inputFile = filesystem.getPathForRelativePath(inputPath);
    Path outputFile = filesystem.getPathForRelativePath(outputPath);
    try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(Files.newInputStream(inputFile)));
        CustomZipOutputStream out = ZipOutputStreams.newOutputStream(outputFile)) {
        for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
            CustomZipEntry customEntry = new CustomZipEntry(entry);
            if (entries.contains(customEntry.getName())) {
                customEntry.setCompressionLevel(compressionLevel.getValue());
            }
            InputStream toUse;
            // If we're using STORED files, we must pre-calculate the CRC.
            if (customEntry.getMethod() == ZipEntry.STORED) {
                try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                    ByteStreams.copy(in, bos);
                    byte[] bytes = bos.toByteArray();
                    customEntry.setCrc(Hashing.crc32().hashBytes(bytes).padToLong());
                    customEntry.setSize(bytes.length);
                    customEntry.setCompressedSize(bytes.length);
                    toUse = new ByteArrayInputStream(bytes);
                }
            } else {
                toUse = in;
            }
            out.putNextEntry(customEntry);
            ByteStreams.copy(toUse, out);
            out.closeEntry();
        }
        return StepExecutionResult.SUCCESS;
    } catch (IOException e) {
        context.logError(e, "Unable to repack zip");
        return StepExecutionResult.ERROR;
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with ZipInputStream

use of java.util.zip.ZipInputStream in project buck by facebook.

the class AaptPackageResourcesIntegrationTest method testAaptPackageIsScrubbed.

@Test
public void testAaptPackageIsScrubbed() throws IOException {
    AssumeAndroidPlatform.assumeSdkIsAvailable();
    workspace.runBuckBuild(MAIN_BUILD_TARGET).assertSuccess();
    Path aaptOutput = workspace.getPath(BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(MAIN_BUILD_TARGET).withFlavors(AndroidBinaryGraphEnhancer.AAPT_PACKAGE_FLAVOR), AaptPackageResources.RESOURCE_APK_PATH_FORMAT));
    Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME));
    try (ZipInputStream is = new ZipInputStream(new FileInputStream(aaptOutput.toFile()))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            assertThat(entry.getName(), new Date(entry.getTime()), Matchers.equalTo(dosEpoch));
        }
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with ZipInputStream

use of java.util.zip.ZipInputStream in project druid by druid-io.

the class CompressionUtils method unzip.

/**
   * Unzip from the input stream to the output directory, using the entry's file name as the file name in the output directory.
   * The behavior of directories in the input stream's zip is undefined.
   * If possible, it is recommended to use unzip(ByteStream, File) instead
   *
   * @param in     The input stream of the zip data. This stream is closed
   * @param outDir The directory to copy the unzipped data to
   *
   * @return The FileUtils.FileCopyResult containing information on all the files which were written
   *
   * @throws IOException
   */
public static FileUtils.FileCopyResult unzip(InputStream in, File outDir) throws IOException {
    try (final ZipInputStream zipIn = new ZipInputStream(in)) {
        final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult();
        ZipEntry entry;
        while ((entry = zipIn.getNextEntry()) != null) {
            final File file = new File(outDir, entry.getName());
            Files.asByteSink(file).writeFrom(zipIn);
            result.addFile(file);
            zipIn.closeEntry();
        }
        return result;
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 4 with ZipInputStream

use of java.util.zip.ZipInputStream in project tomcat by apache.

the class SignCode method extractFilesFromApplicationString.

/**
     * Removes base64 encoding, unzips the files and writes the new files over
     * the top of the old ones.
     */
private static void extractFilesFromApplicationString(String data, List<File> files) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(data));
    try (ZipInputStream zis = new ZipInputStream(bais)) {
        byte[] buf = new byte[32 * 1024];
        for (int i = 0; i < files.size(); i++) {
            try (FileOutputStream fos = new FileOutputStream(files.get(i))) {
                zis.getNextEntry();
                int numRead;
                while ((numRead = zis.read(buf)) >= 0) {
                    fos.write(buf, 0, numRead);
                }
            }
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream)

Example 5 with ZipInputStream

use of java.util.zip.ZipInputStream in project cw-omnibus by commonsguy.

the class ZipUtils method unzip.

public static void unzip(File zipFile, File destDir, String subtreeInZip) throws UnzipException, IOException {
    if (destDir.exists()) {
        deleteContents(destDir);
    } else {
        destDir.mkdirs();
    }
    try {
        final FileInputStream fis = new FileInputStream(zipFile);
        final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        int entries = 0;
        long total = 0;
        try {
            while ((entry = zis.getNextEntry()) != null) {
                if (subtreeInZip == null || entry.getName().startsWith(subtreeInZip)) {
                    int bytesRead;
                    final byte[] data = new byte[BUFFER_SIZE];
                    final String zipCanonicalPath = validateZipEntry(entry.getName().substring(subtreeInZip.length()), destDir);
                    if (entry.isDirectory()) {
                        new File(zipCanonicalPath).mkdir();
                    } else {
                        final FileOutputStream fos = new FileOutputStream(zipCanonicalPath);
                        final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
                        while (total + BUFFER_SIZE <= DEFAULT_MAX_SIZE && (bytesRead = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                            dest.write(data, 0, bytesRead);
                            total += bytesRead;
                        }
                        dest.flush();
                        fos.getFD().sync();
                        dest.close();
                        if (total + BUFFER_SIZE > DEFAULT_MAX_SIZE) {
                            throw new IllegalStateException("Too much output from ZIP");
                        }
                    }
                    zis.closeEntry();
                    entries++;
                    if (entries > DEFAULT_MAX_ENTRIES) {
                        throw new IllegalStateException("Too many entries in ZIP");
                    }
                }
            }
        } finally {
            zis.close();
        }
    } catch (Throwable t) {
        if (destDir.exists()) {
            delete(destDir);
        }
        throw new UnzipException("Problem in unzip operation, rolling back", t);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43