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:Main.java

/**
 * creates azip file from an directory with all subfolders
 * @param dir//from ww  w  .j  a v  a  2 s  .c  o  m
 * @param zipFileName
 * @throws IOException
 */
public static void createZipFile(String dir, String zipFileName) throws IOException {
    String dirFile = dir + "/" + zipFileName;
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dirFile)));
    try {
        zipDir(dir, zipOut, zipFileName);
    } finally {
        zipOut.close();
    }
}

From source file:Main.java

public static boolean compress(File file) {
    try {/*from   w  w w .  j  ava 2 s . c o  m*/
        String fileName = file.getName();
        if (fileName.indexOf(".") != -1)
            fileName = fileName.substring(0, fileName.indexOf("."));
        FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
        CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        InputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getName()));
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);
        out.closeEntry();

        in.close();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Compress a String to a zip file that has only one entry like zipName
 * The name shouldn't have .zip/*w w w  .  j a v a 2  s  . c o  m*/
 * 
 * @param content
 * @param fName
 * @throws IOException 
 */
public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset));
    BufferedInputStream bis = new BufferedInputStream(bais);

    File f = new File(fName);
    File parentFile = f.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip")));

    ZipEntry entry = new ZipEntry(f.getName());
    zf.putNextEntry(entry);
    byte[] barr = new byte[8192];
    int len = 0;
    while ((len = bis.read(barr)) != -1) {
        zf.write(barr, 0, len);
    }
    zf.flush();
    zf.close();
    bis.close();
    bais.close();
}

From source file:Main.java

/**
 * Target is the zip to create//www . j  av  a  2s  . c  o m
 * Source is the file or directory to zip
 * 
 * @param zipTarget
 * @param source
 * @param m
 * @throws IOException
 */
static public void compressDirectory(File zipTarget, File source) throws IOException {

    // create a ZipOutputStream to zip the data to.
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipTarget));
    try {
        if (source.isDirectory()) {
            zipDir(source, zos, source.getName());
        } else
            zipFile(source, zos, source.getName());
    } finally {// close the stream
        zos.close();
    }
}

From source file:Main.java

public static String zip(String filename) throws IOException {
    BufferedInputStream origin = null;
    Integer BUFFER_SIZE = 20480;/*from   w  w w .j a  va  2  s.c  o  m*/

    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
        String compressedFile = flockedFilesFolder.toString() + "/" + "flockZip.zip";
        ZipOutputStream out = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(compressedFile)));
        out.setLevel(9);
        try {
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fi = new FileInputStream(uncompressedFile);
            System.out.println("Filename: " + uncompressedFile);
            System.out.println("Zipfile: " + compressedFile);
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(
                        uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                origin.close();
            }
        } finally {
            out.close();
        }
        return "flockZip.zip";
    }
    return null;
}

From source file:ZipHelper.java

public static void fileToZip(File file, File zipFile, int compressionLevel) throws Exception {
    zipFile.createNewFile();//  www  .  j a  v  a2 s  . co  m
    FileOutputStream fout = new FileOutputStream(zipFile);
    ZipOutputStream zout = null;
    try {
        zout = new ZipOutputStream(new BufferedOutputStream(fout));
        zout.setLevel(compressionLevel);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++)
                fileToZip(files[i], zout, file);
        } else if (file.isFile()) {
            fileToZip(file, zout, file.getParentFile());
        }
    } finally {
        if (zout != null)
            zout.close();
    }
}

From source file:es.caib.sgtsic.util.Zips.java

public static DataHandler generateZip(Map<String, DataHandler> documents) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(baos);

    for (String key : documents.keySet()) {

        InputStream is = new ByteArrayInputStream(DataHandlers.dataHandlerToByteArray(documents.get(key)));
        ZipEntry zipEntry = new ZipEntry(key);
        zip.putNextEntry(zipEntry);//w ww.  j  a va 2s  .  c  o m
        IOUtils.copy(is, zip);
        zip.closeEntry();
        is.close();
    }

    zip.close();
    baos.close();

    return DataHandlers.byteArrayToDataHandler(baos.toByteArray(), "application/zip");

}

From source file:Compress.java

/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
        throw new IllegalArgumentException("Not a directory:  " + dir);
    String[] entries = d.list();/*from www.j a  v  a 2 s  .  c om*/
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

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

    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue;//Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }
    out.close();
}

From source file:Main.java

public static void zipDir(String zipFileName, String dir) throws Exception {
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFileName));

    zipDir(dir, zipOut);/* w w w  . j  a va  2s.c om*/

    zipOut.close();
}

From source file:com.microsoft.azurebatch.jenkins.utils.ZipHelper.java

/**
 * Zip folder/*from w w  w.  ja  v  a 2s. c o m*/
 * @param srcFolderPath source folder path
 * @param outputZipPath output zip file path
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void zipFolder(String srcFolderPath, String outputZipPath)
        throws FileNotFoundException, IOException {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    // Create the output stream to zip file result
    fileWriter = new FileOutputStream(outputZipPath);
    zip = new ZipOutputStream(fileWriter);

    // Add the folder to the zip
    addFolderToZip("", srcFolderPath, zip);

    // Close the zip objects
    zip.flush();
    zip.close();
}