一、压缩文件
public static void zipFile() throws IOException{
File file = new File("D:/hello.txt");
File zipFile = new File("D:/hello.zip");
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(out);
zipOut.putNextEntry(new ZipEntry("Zip"));
byte[] buffer = new byte[1024];
int len=0;
while((len=in.read(buffer))!=-1){
zipOut.write(buffer,0, len);
}
zipOut.close();
out.close();
in.close();
}
二、解压文件
public static void unZipFile() throws IOException{
ZipFile zipFile = new ZipFile("D:/hello.zip");
File file = new File("D:/hello.txt");
InputStream in=null;
FileOutputStream out =null;
ZipEntry entry;
for(Enumeration entries = zipFile.entries();entries.hasMoreElements();){
entry = (ZipEntry)entries.nextElement();
in =zipFile.getInputStream(entry);
out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len=0;
while((len=in.read(buffer))!=-1){
out.write(buffer,0, len);
}
}
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}