use of java.util.zip.ZipEntry in project buck by facebook.
the class JavaInMemoryFileObject method openOutputStream.
@Override
public synchronized OutputStream openOutputStream() throws IOException {
if (isOpened) {
throw new IOException(ALREADY_OPENED);
}
isOpened = true;
final ZipEntry entry = JavaInMemoryFileManager.createEntry(getName());
return new OutputStream() {
@Override
public void write(int b) throws IOException {
bos.write(b);
}
@Override
public void close() throws IOException {
bos.close();
jarFileSemaphore.acquireUninterruptibly();
try {
jarOutputStream.putNextEntry(entry);
jarOutputStream.write(bos.toByteArray());
jarOutputStream.closeEntry();
isWritten = true;
} finally {
jarFileSemaphore.release();
}
}
};
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class DxAnalysisMain method loadAllClasses.
private static ImmutableMap<String, ClassNode> loadAllClasses(String zipFileName) throws IOException {
ImmutableMap.Builder<String, ClassNode> allClassesBuilder = ImmutableMap.builder();
try (ZipFile inJar = new ZipFile(zipFileName)) {
for (ZipEntry entry : Collections.list(inJar.entries())) {
if (!entry.getName().endsWith(".class")) {
continue;
}
// Skip external libraries.
if (entry.getName().startsWith("junit/") || entry.getName().startsWith("org/junit/") || entry.getName().startsWith("com/google/common/")) {
continue;
}
byte[] rawClass = ByteStreams.toByteArray(inJar.getInputStream(entry));
ClassNode klass = new ClassNode();
new ClassReader(rawClass).accept(klass, ClassReader.EXPAND_FRAMES);
allClassesBuilder.put(klass.name, klass);
}
}
return allClassesBuilder.build();
}
use of java.util.zip.ZipEntry in project elasticsearch by elastic.
the class JarHellTests method makeJar.
URL makeJar(Path dir, String name, Manifest manifest, String... files) throws IOException {
Path jarpath = dir.resolve(name);
ZipOutputStream out;
if (manifest == null) {
out = new JarOutputStream(Files.newOutputStream(jarpath, StandardOpenOption.CREATE));
} else {
out = new JarOutputStream(Files.newOutputStream(jarpath, StandardOpenOption.CREATE), manifest);
}
for (String file : files) {
out.putNextEntry(new ZipEntry(file));
}
out.close();
return jarpath.toUri().toURL();
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class ZipWriteTest method main.
public static void main(String[] args) throws IOException, InterruptedException {
try (CustomZipOutputStream zipOut = ZipOutputStreams.newOutputStream(Paths.get("/dev/null"), ZipOutputStreams.HandleDuplicates.APPEND_TO_ZIP)) {
try (ZipFile zipIn = new ZipFile(new File(args[0]))) {
for (Enumeration<? extends ZipEntry> entries = zipIn.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = entries.nextElement();
ZipEntry newEntry = new ZipEntry(entry);
if (entry.getMethod() == ZipEntry.DEFLATED) {
newEntry.setCompressedSize(-1);
}
zipOut.putNextEntry(newEntry);
InputStream inputStream = zipIn.getInputStream(entry);
ByteStreams.copy(inputStream, zipOut);
zipOut.closeEntry();
}
}
}
System.gc();
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
System.gc();
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
}
use of java.util.zip.ZipEntry 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;
}
}
Aggregations