Example usage for java.util.zip ZipOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP output stream as well as the stream being filtered.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/MyZip.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);
    ZipEntry ze = new ZipEntry("C:/file1.txt");
    zos.putNextEntry(ze);/*from  w  w w  .j ava2  s.co m*/
    zos.closeEntry();
    ze = new ZipEntry("C:/file2.txt");
    zos.putNextEntry(ze);
    zos.closeEntry();
    zos.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String outputFile = "a.zip";
    int level = 9;
    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);//ww  w .java 2  s .co m

    ZipEntry ze = new ZipEntry("a.zip");
    FileInputStream fin = new FileInputStream("b.dat");
    zout.putNextEntry(ze);
    for (int c = fin.read(); c != -1; c = fin.read()) {
        zout.write(c);
    }
    fin.close();
    zout.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileInputStream inStream = new FileInputStream("test.txt");
    ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream("compressed.zip"));

    outStream.putNextEntry(new ZipEntry("test.txt"));

    byte[] buffer = new byte[1024];
    int bytesRead;

    while ((bytesRead = inStream.read(buffer)) > 0) {
        outStream.write(buffer, 0, bytesRead);
    }//  w ww  .java2s . c om

    outStream.closeEntry();
    outStream.close();
    inStream.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] filenames = new String[] { "filename1", "filename2" };
    byte[] buf = new byte[1024];
    String outFilename = "outfile.zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

    for (int i = 0; i < filenames.length; i++) {
        FileInputStream in = new FileInputStream(filenames[i]);
        out.putNextEntry(new ZipEntry(filenames[i]));
        int len;/* ww w  . ja va  2s . c o m*/
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
}

From source file:com.bright.utils.ZipFile.java

public static void main(String[] args) {

    try {/*from   w w w  .j a  va  2 s .  co m*/
        // name of zip file to create
        String outFilename = args[1];

        // create ZipOutputStream object
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

        // path to the folder to be zipped
        File zipFolder = new File(args[0]);

        int len = zipFolder.getAbsolutePath().lastIndexOf(File.separator);
        String baseName = zipFolder.getAbsolutePath().substring(0, len + 1);

        addFolderToZip(zipFolder, out, baseName);
        System.out.println("Created ZIP file: " + args[1]);

        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String outputFile = "new.zip";
    int level = 9;
    int start = 1;

    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);/*from   w ww  .j a v a2  s.  co  m*/
    for (int i = start; i < args.length; i++) {
        ZipEntry ze = new ZipEntry(args[i]);
        FileInputStream fin = new FileInputStream(args[i]);
        try {
            System.out.println("Compressing " + args[i]);
            zout.putNextEntry(ze);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                zout.write(c);
            }
        } finally {
            fin.close();
        }
    }
    zout.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    String outputFile = "new.zip";
    // Default to maximum compression
    int level = 9;
    int start = 1;

    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);/*from   www  .ja v a 2  s.c o  m*/
    for (int i = start; i < args.length; i++) {
        ZipEntry ze = new ZipEntry(args[i]);
        FileInputStream fin = new FileInputStream(args[i]);
        try {
            System.out.println("Compressing " + args[i]);
            zout.putNextEntry(ze);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                zout.write(c);
            }
        } finally {
            fin.close();
        }
    }
    zout.close();
}

From source file:de.jwi.zip.Zipper.java

public static void main(String[] args) throws Exception {
    ZipOutputStream z = new ZipOutputStream(new FileOutputStream("d:/temp/myfirst.zip"));

    IOFileFilter filter = new IOFileFilter() {

        public boolean accept(java.io.File file) {
            return true;
        }/* w  ww . ja  va  2s  . c o m*/

        public boolean accept(java.io.File dir, java.lang.String name) {
            return true;
        }
    };

    Collection c = FileUtils.listFiles(new File("/java/javadocs/j2sdk-1.4.1/docs/tooldocs"), filter, filter);

    new Zipper().zip(z, c, new File("/java/javadocs/j2sdk-1.4.1"));

    z.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CheckedOutputStream checksum = new CheckedOutputStream(new FileOutputStream("data.zip"), new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));

    int size = 0;
    byte[] buffer = new byte[1024];

    File dir = new File(".");
    String[] files = dir.list();/*from www . j  av a  2s  .  co m*/

    for (int i = 0; i < files.length; i++) {
        System.out.println("Compressing: " + files[i]);
        FileInputStream fis = new FileInputStream(files[i]);
        ZipEntry zipEntry = new ZipEntry(files[i]);
        zos.putNextEntry(zipEntry);

        while ((size = fis.read(buffer, 0, buffer.length)) > 0) {
            zos.write(buffer, 0, size);
        }
        zos.closeEntry();
        fis.close();
    }
    zos.close();
    System.out.println("Checksum   : " + checksum.getChecksum().getValue());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0])));

    for (int n = 1; n < args.length; n++) {

        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n]));
        ZipEntry ze = new ZipEntry(rmPath(args[n]));

        fout.putNextEntry(ze);//from  w  ww .  j a  va 2s .c o  m

        int i;
        do {
            i = fin.read();
            if (i != -1)
                fout.write(i);
        } while (i != -1);

        fout.closeEntry();
        fin.close();

        System.out.println("Compressing " + args[n]);
        System.out.println(
                " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n");
    }
    fout.close();
}