Java ZipEntry.setMethod(int method)

Syntax

ZipEntry.setMethod(int method) has the following syntax.

public void setMethod(int method)

Example

In the following code shows how to use ZipEntry.setMethod(int method) method.


//from   w  ww .jav  a2s.  c o m

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    Main list = new Main();
    list.doZip("a.txt", "a.zip");
  }

  public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setMethod(ZipEntry.DEFLATED);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
  }
}




















Home »
  Java Tutorial »
    java.util.zip »




ZipEntry
ZipFile