Example usage for java.util.zip ZipOutputStream ZipOutputStream

List of usage examples for java.util.zip ZipOutputStream ZipOutputStream

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream ZipOutputStream.

Prototype

public ZipOutputStream(OutputStream out) 

Source Link

Document

Creates a new ZIP output stream.

Usage

From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java

private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;//from w  w  w. j ava2 s.com
    ZipOutputStream outJar = null;

    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }

        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }

        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }

            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();

            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);

            byte[] entryData = entryBuffer.toByteArray();

            String entryName = entry.getName();

            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');

                for (AccessTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }

            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}

From source file:com.serotonin.mango.rt.maint.work.ReportWorkItem.java

private void addFileAttachment(EmailContent emailContent, String name, File file) {
    if (file != null) {
        if (reportConfig.isZipData()) {
            try {
                File zipFile = File.createTempFile("tempZIP", ".zip");
                ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
                zipOut.putNextEntry(new ZipEntry(name));

                FileInputStream in = new FileInputStream(file);
                StreamUtils.transfer(in, zipOut);
                in.close();//ww w . j a v a 2  s .  c o m

                zipOut.closeEntry();
                zipOut.close();

                emailContent.addAttachment(new EmailAttachment.FileAttachment(name + ".zip", zipFile));

                filesToDelete.add(zipFile);
            } catch (IOException e) {
                LOG.error("Failed to create zip file", e);
            }
        } else
            emailContent.addAttachment(new EmailAttachment.FileAttachment(name, file));

        filesToDelete.add(file);
    }
}

From source file:edu.mayo.pipes.iterators.Compressor.java

/**
 * Create a single entry Zip archive, and prepare it for writing
 *
 * @throws IOException//w  w  w. j a va 2  s  .co  m
 */
public BufferedWriter makeZipWriter() throws IOException {
    if (outFile == null)
        return null;

    FileOutputStream outFileStream = new FileOutputStream(outFile);
    ZipOutputStream zipWrite = new ZipOutputStream(outFileStream);
    ZipEntry zE;

    // Setup the zip writing things
    zipWrite.setMethod(ZipOutputStream.DEFLATED);
    zipWrite.setLevel(9); // Max compression
    zE = new ZipEntry("Default");
    zipWrite.putNextEntry(zE);
    // Now can attach the writer to write to this zip entry
    OutputStreamWriter wStream = new OutputStreamWriter(zipWrite);
    writer = new BufferedWriter(wStream);
    comp = kZipCompression;
    return writer;
}

From source file:com.mosso.client.cloudfiles.sample.FilesCopy.java

/**
 *
 * @param folder//from   w  w  w .j a  v a  2  s .  co  m
 * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped.
 * @throws IOException
 */
public static File zipFolder(File folder) throws IOException {
    byte[] buf = new byte[1024];
    int len;

    // Create the ZIP file
    String filenameWithZipExt = folder.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));

    if (folder.isDirectory()) {
        File[] files = folder.listFiles();

        for (File f : files) {
            if (!f.isDirectory()) {
                FileInputStream in = new FileInputStream(f);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(f.getName()));

                // Transfer bytes from the file to the ZIP file
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            } else
                logger.warn("Skipping nested folder: " + f.getAbsoluteFile());
        }

        out.flush();
        out.close();
    } else {
        logger.warn("The folder name supplied is not a folder!");
        System.err.println("The folder name supplied is not a folder!");
        return null;
    }

    return zippedFile;
}

From source file:com.eviware.soapui.testondemand.TestOnDemandCaller.java

private static byte[] zipBytes(String filename, byte[] dataToBeZiped) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zipedOutputStream = new ZipOutputStream(outputStream);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(dataToBeZiped.length);
    try {/* w ww.jav  a 2  s  . c  om*/
        zipedOutputStream.putNextEntry(entry);
        zipedOutputStream.write(dataToBeZiped);
    } finally {
        zipedOutputStream.closeEntry();
        zipedOutputStream.close();
    }
    return outputStream.toByteArray();
}