Search in sources :

Example 6 with ZipEntry

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

the class SignCode method getApplicationString.

/**
     * Zips the files, base 64 encodes the resulting zip and then returns the
     * string. It would be far more efficient to stream this directly to the
     * signing server but the files that need to be signed are relatively small
     * and this simpler to write.
     *
     * @param fileNames Modified names of files
     * @param files     Files to be signed
     */
private static String getApplicationString(List<String> fileNames, List<File> files) throws IOException {
    // 16 MB should be more than enough for Tomcat
    // TODO: Refactoring this entire class so it uses streaming rather than
    //       buffering the entire set of files in memory would make it more
    //       widely useful.
    ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024 * 1024);
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        byte[] buf = new byte[32 * 1024];
        for (int i = 0; i < files.size(); i++) {
            try (FileInputStream fis = new FileInputStream(files.get(i))) {
                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
                zos.putNextEntry(zipEntry);
                int numRead;
                while ((numRead = fis.read(buf)) >= 0) {
                    zos.write(buf, 0, numRead);
                }
            }
        }
    }
    return Base64.encodeBase64String(baos.toByteArray());
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream)

Example 7 with ZipEntry

use of java.util.zip.ZipEntry 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)

Example 8 with ZipEntry

use of java.util.zip.ZipEntry in project cglib by cglib.

the class AbstractTransformTask method processJarFile.

protected void processJarFile(File file) throws Exception {
    if (verbose) {
        log("processing " + file.toURI());
    }
    File tempFile = File.createTempFile(file.getName(), null, new File(file.getAbsoluteFile().getParent()));
    try {
        ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
        try {
            FileOutputStream fout = new FileOutputStream(tempFile);
            try {
                ZipOutputStream out = new ZipOutputStream(fout);
                ZipEntry entry;
                while ((entry = zip.getNextEntry()) != null) {
                    byte[] bytes = getBytes(zip);
                    if (!entry.isDirectory()) {
                        DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));
                        if (din.readInt() == CLASS_MAGIC) {
                            bytes = process(bytes);
                        } else {
                            if (verbose) {
                                log("ignoring " + entry.toString());
                            }
                        }
                    }
                    ZipEntry outEntry = new ZipEntry(entry.getName());
                    outEntry.setMethod(entry.getMethod());
                    outEntry.setComment(entry.getComment());
                    outEntry.setSize(bytes.length);
                    if (outEntry.getMethod() == ZipEntry.STORED) {
                        CRC32 crc = new CRC32();
                        crc.update(bytes);
                        outEntry.setCrc(crc.getValue());
                        outEntry.setCompressedSize(bytes.length);
                    }
                    out.putNextEntry(outEntry);
                    out.write(bytes);
                    out.closeEntry();
                    zip.closeEntry();
                }
                out.close();
            } finally {
                fout.close();
            }
        } finally {
            zip.close();
        }
        if (file.delete()) {
            File newFile = new File(tempFile.getAbsolutePath());
            if (!newFile.renameTo(file)) {
                throw new IOException("can not rename " + tempFile + " to " + file);
            }
        } else {
            throw new IOException("can not delete " + file);
        }
    } finally {
        tempFile.delete();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 9 with ZipEntry

use of java.util.zip.ZipEntry in project che by eclipse.

the class ZipUtils method unzip.

public static void unzip(InputStream in, File targetDir) throws IOException {
    final ZipInputStream zipIn = new ZipInputStream(in);
    final byte[] b = new byte[BUF_SIZE];
    ZipEntry zipEntry;
    while ((zipEntry = zipIn.getNextEntry()) != null) {
        final File file = new File(targetDir, zipEntry.getName());
        if (!zipEntry.isDirectory()) {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create parent folder " + parent.getAbsolutePath());
                }
            }
            try (FileOutputStream fos = new FileOutputStream(file)) {
                int r;
                while ((r = zipIn.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
            }
        } else {
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new IOException("Unable to create folder " + file.getAbsolutePath());
                }
            }
        }
        zipIn.closeEntry();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 10 with ZipEntry

use of java.util.zip.ZipEntry in project che by eclipse.

the class ZipUtils method addFileEntry.

private static void addFileEntry(ZipOutputStream zipOut, String entryName, File file) throws IOException {
    final ZipEntry zipEntryEntry = new ZipEntry(entryName);
    zipOut.putNextEntry(zipEntryEntry);
    try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
        final byte[] buf = new byte[BUF_SIZE];
        int r;
        while ((r = in.read(buf)) != -1) {
            zipOut.write(buf, 0, r);
        }
    }
    zipOut.closeEntry();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream)

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