Search in sources :

Example 41 with ZipEntry

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();
            }
        }
    };
}
Also used : ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) IOException(java.io.IOException)

Example 42 with ZipEntry

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();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 43 with ZipEntry

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();
}
Also used : Path(java.nio.file.Path) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream)

Example 44 with ZipEntry

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));
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 45 with ZipEntry

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

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56