<pre name="code" class="java">package com.io.zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class DeZipFile {
public static void main(String[] args) throws Exception {
String filepath = "F:\\z321.zip";
String destpath = "F:\\dezip";
// test1(filepath);
test2(filepath, destpath);
}
/**
* 查看压缩包下面有哪些文件
*
* @param filepath
* @throws IOException
*/
private static void test1(String filepath) throws IOException {
ZipFile zf = new ZipFile(filepath);
Enumeration enums = zf.entries();
while (enums.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enums.nextElement();
System.out.println("文件:" + entry.getName());
}
}
/**
* 解压filepath下面的文件到destpath路径下面
*
* @param filepath
* @param destpath
* @throws IOException
*/
private static void test2(String filepath, String destpath)
throws IOException {
File d = new File(destpath);
if(!d.exists()){
d.mkdirs();
}
FileInputStream fis = new FileInputStream(filepath);
CheckedInputStream cis = new CheckedInputStream(fis, new Adler32());
ZipInputStream zis = new ZipInputStream(cis);
FileOutputStream out;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
out = new FileOutputStream(destpath + File.separator + ze.getName());
int x;
while ((x = zis.read()) != -1) {
out.write(x);
}
out.close();
}
zis.close();
}
}
package com.io.zip;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FilenameFilter;import java.io.IOException;import java.nio.charset.Charset;import java.util.regex.Pattern;import java.util.zip.Adler32;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipManyFile {public static void main(String[] args) throws Exception {// String file1 = "E:\\cookie.java",file2 = "F:\\core.log",file3 = "E:\\wold.png";// zipfiles(file1,file2,file3);//压缩整个文件下的文件String filedir = "F:\\testzip";zipfiles2(filedir);}<span style="white-space:pre"> </span>
private static void zipfiles(String... files) throws IOException {
FileOutputStream fos = new FileOutputStream("F:\\z321.zip");
CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
zos.setComment("z321test测试");
for (String fileName : files) {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(file.getName()));
int x;
while ((x = fis.read()) != -1) {
zos.write(x);
}
fis.close();
}
zos.close();
System.out.println("checksum: " + cos.getChecksum().getValue());
}
private static void zipfiles2(String filedir) throws IOException {
FileOutputStream fos = new FileOutputStream("F:\\some3333.zip");
CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32());
ZipOutputStream zos = new ZipOutputStream(cos, Charset.forName("gb2312"));
zos.setComment("测试压缩一个文件下的所有文件haha");
BufferedOutputStream bos = new BufferedOutputStream(zos);
final String reg = "[hH].*";
File file = new File(filedir);
File[] fs = file.listFiles(new FilenameFilter() {
private Pattern pattern = Pattern.compile(reg);
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
for(File f : fs){
// FileInputStream fis = new FileInputStream(f);
BufferedReader br = new BufferedReader(new FileReader(f));
zos.putNextEntry(new ZipEntry(file.getName()+"\\"+f.getName()));
int x;
while((x=br.read())!=-1){
bos.write(x);
}
br.close();
}
bos.close();
System.out.println("checksum: "+cos.getChecksum().getValue());
}
}
</pre><pre name="code" class="java">