Java by API/java.util.zip/ZipEntry — различия между версиями

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

Текущая версия на 14:22, 31 мая 2010

new ZipEntry(String name)

 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
  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;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      out.closeEntry();
      in.close();
    }
    out.close();
  }
}





ZipEntry.DEFLATED

 
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Main {
  public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("your.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
      ZipEntry ze = (ZipEntry) e.nextElement();
      String name = ze.getName();
      Date lastModified = new Date(ze.getTime());
      long uncompressedSize = ze.getSize();
      long compressedSize = ze.getCompressedSize();
      int method = ze.getMethod();
      if (method == ZipEntry.STORED) {
        System.out.println(name + " was stored at " + lastModified);
        System.out.println("with a size of  " + uncompressedSize + " bytes");
      } else if (method == ZipEntry.DEFLATED) {
        System.out.println(name + " was deflated at " + lastModified);
        System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
            + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
      } else {
        System.out.println(name + " was compressed at " + lastModified);
        System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
            + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
      }
    }
  }
}





ZipEntry: getComment()

 
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Main {
  public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("your.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
      ZipEntry ze = (ZipEntry) e.nextElement();
      String name = ze.getName();
      long crc = ze.getCrc();
      System.out.println("Its CRC is " + crc);
      String comment = ze.getComment();
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (ze.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}





ZipEntry: getCompressedSize()

 
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Main {
  public static void main(String[] args) {
    try {
      ZipFile zf = new ZipFile("your.zip");
      Enumeration e = zf.entries();
      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        String name = ze.getName();
        Date lastModified = new Date(ze.getTime());
        long uncompressedSize = ze.getSize();
        long compressedSize = ze.getCompressedSize();
        System.out.println(name);
        System.out.println(lastModified);
        System.out.println(uncompressedSize);
        System.out.println(compressedSize);
      }
    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
}





ZipEntry: getCrc()

 
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Main {
  public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("your.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
      ZipEntry ze = (ZipEntry) e.nextElement();
      String name = ze.getName();
      long crc = ze.getCrc();
      System.out.println("Its CRC is " + crc);
      String comment = ze.getComment();
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (ze.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}





ZipEntry: getName()

 
import java.io.FileInputStream;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(args[0]);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null)
      System.out.println(ze.getName() + "  [" + ze.getSize() + "]  ["
          + new Date(ze.getTime()).toString() + "]");
  }
}





ZipEntry: getSize()

 
import java.io.FileInputStream;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(args[0]);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null)
      System.out.println(ze.getName() + "  [" + ze.getSize() + "]  ["
          + new Date(ze.getTime()).toString() + "]");
  }
}





ZipEntry: getTime()

 
import java.io.FileInputStream;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(args[0]);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null)
      System.out.println(ze.getName() + "  [" + ze.getSize() + "]  ["
          + new Date(ze.getTime()).toString() + "]");
  }
}





ZipEntry: isDirectory()

 
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Main {
  public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("your.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
      ZipEntry ze = (ZipEntry) e.nextElement();
      String name = ze.getName();
      long crc = ze.getCrc();
      System.out.println("Its CRC is " + crc);
      String comment = ze.getComment();
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (ze.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}





ZipEntry.STORED

 
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Main {
  public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("your.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
      ZipEntry ze = (ZipEntry) e.nextElement();
      String name = ze.getName();
      Date lastModified = new Date(ze.getTime());
      long uncompressedSize = ze.getSize();
      long compressedSize = ze.getCompressedSize();
      int method = ze.getMethod();
      if (method == ZipEntry.STORED) {
        System.out.println(name + " was stored at " + lastModified);
        System.out.println("with a size of  " + uncompressedSize + " bytes");
      } else if (method == ZipEntry.DEFLATED) {
        System.out.println(name + " was deflated at " + lastModified);
        System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
            + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
      } else {
        System.out.println(name + " was compressed at " + lastModified);
        System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
            + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
      }
    }
  }
}