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;
}
}
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));
}
}
}
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;
}
}
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);
}
}
}
}
}
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);
}
}
Aggregations