Java by API/java.util.zip/ZipOutputStream

Материал из Java эксперт
Перейти к: навигация, поиск

new ZipOutputStream(OutputStream out)

 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
  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);
    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();
  }
}





ZipOutputStream: close()

 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
  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);
    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();
  }
}





ZipOutputStream: closeEntry()

 
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
  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);
    zos.closeEntry();
    ze = new ZipEntry("C:/file2.txt");
    zos.putNextEntry(ze);
    zos.closeEntry();
    zos.close();
  }
}





ZipOutputStream: putNextEntry(ZipEntry e)

 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Main {
  public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    for (int i = 0; i < args.length; i++) {
      System.out.println("Writing file " + args[i]);
      BufferedReader in = new BufferedReader(new FileReader(args[i]));
      zos.putNextEntry(new ZipEntry(args[i]));
      int c;
      while ((c = in.read()) != -1)
        out.write(c);
      in.close();
    }
    out.close();

    System.out.println("Checksum: " + csum.getChecksum().getValue());
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
      System.out.println("Reading file " + ze);
      int x;
      while ((x = bis.read()) != -1)
        System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();

  }
}





ZipOutputStream: setComment(String comment)

 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Main {
  public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    for (int i = 0; i < args.length; i++) {
      System.out.println("Writing file " + args[i]);
      BufferedReader in = new BufferedReader(new FileReader(args[i]));
      zos.putNextEntry(new ZipEntry(args[i]));
      int c;
      while ((c = in.read()) != -1)
        out.write(c);
      in.close();
    }
    out.close();

    System.out.println("Checksum: " + csum.getChecksum().getValue());
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
      System.out.println("Reading file " + ze);
      int x;
      while ((x = bis.read()) != -1)
        System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();

  }
}