Reading ZIP Files
http://javaboutique.internet.com/resources/books/JavaNut/javanut2_6.html
Unzipping Files with java.util.zip.ZipFile
http://gethelp.devx.com/techtips/java_pro/10MinuteSolutions/10min0300.asp
basially, you do
InputStream st = zipFile.getInputStream(zipFile.getEntry(entryName));
for example, from the first link:
import java.io.*;
import java.util.zip.*;
String filename; // File to read; initialized elsewhere
String entryname; // Entry to read from the ZIP file; initialized elsewhere
ZipFile zipfile = new ZipFile(filename); // Open the ZIP file
ZipEntry entry = zipfile.getEntry(entryname); // Get one entry
InputStream in = zipfile.getInputStream(entry); // A stream to read the entry
BufferedInputStream bis = new BufferedInputStream(in); // Improves efficiency
ZipFile zipfile = new ZipFile("figs.zip");
使用条目方法,返回Enumeration对象,以循环通过文件全 部的ZipEntry对象:
while(e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
// read contents and save them
}
通过将 ZipEntry传递到getInputStream的 方式,读取 ZIP 文件内部具体的ZipEntry,它将返回可以读取 条目内容的InputStream对象:
is = new
BufferedInputStream(zipfile.getInputStream(entry));
检索条目的文件名并创建输出流,以便保存:
byte data[] = new byte[BUFFER];
FileOutputStream fos = new
FileOutputStream(entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
public class UnZip2 {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(argv[0]);
Enumeration e = zipfile.entries();
while(e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
System.out.println("Extracting: " +entry);
is = new BufferedInputStream
(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new
FileOutputStream(entry.getName());
dest = new
BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER))
!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
压缩和归档 ZIP 文件中的数据
ZipOutputStream可用于将数据压缩到ZIP文件。 ZipOutputStream以ZIP格式将数据写入输出流。创建ZIP文件的步 骤很多。
第一步是创建ZipOutputStream对象,将向它传递希望写入 文件的输出流。创建名为“myfigs.zip”的ZIP文件的方式是:
FileOutputStream dest = new
FileOutputStream("myfigs.zip");
ZipOutputStream out = new
ZipOutputStream(new BufferedOutputStream(dest));
一旦创建了目标压缩输出流,下一步就是打开源数据文件。本例中的源数 据文件在当前目录中。列表命令用来获取当前目录文件的列表:
File f = new File(".");
String files[] = f.list();
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new FileInputStream(files[i]);
// create zip entry
// add entries to ZIP file
}
public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("c:\\zip\\myfigs.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File(".");
String files[] = f.list();
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream(files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
代码样本 1 中的源程序介绍了如何从 ZIP 文件中解压和提取文件。要测试 此样本,可对类进行编译,并通过传递 ZIP 格式的已压缩文件来进行运行:
prompt> java UnZip somefile.zip
注意,somefile.zip可以是任何ZIP兼容工具,如WinZip,创 建的ZIP文件。
代码样本 1: UnZip.java
import java.io.*;
import java.util.zip.*;
public class UnZip {
final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedOutputStream dest = null;
FileInputStream fis = new
FileInputStream(argv[0]);
ZipInputStream zis = new
ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new
FileOutputStream(entry.getName());
dest = new
BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER))
!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}