ZipFile(如何实现对文件和目录的压缩,解压缩?



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipFile {
public static void main(String[] args) {

}

/**
* 文件压缩
* files:要压缩的文件
* destZip:压缩之后的zip文件所放的目录,需要"D:\\"或"D:\\test\"格式
* */

public static void zip(String[] files, String destZip) throws IOException {
FileOutputStream fos = new FileOutputStream(destZip);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);

//循环读文件,压缩到zip中
for (int i = 0; i < files.length; i++) {
String file = files[i];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
//获取文件名,创建条目并添加到zip中
File f = new File(file);
ZipEntry zl = new ZipEntry(f.getName());
zos.putNextEntry(zl);
//读取文件中的字节信息,压入条目
int tmp = -1;
while ((tmp = bis.read()) != -1) {
zos.write(tmp);
}
zos.closeEntry();
bis.close();
fis.close();
}
zos.close();
bos.close();
fos.close();
}

/**
* 文件解压缩
* zipfile:要解压缩的zip文件
* destpah:解压后文件所放的目录,需要"D:\\"或"D:\\test\"格式
* */
public static void unzip(String zipfile, String destpath) throws IOException {
FileInputStream fis = new FileInputStream("C:\\a.zip");
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry zl = null;
while ((zl = zis.getNextEntry()) != null) {
if (zl.isDirectory()) {
File f = new File("C:\\" + zl.getName());
f.mkdirs();
} else {
String file = zl.getName();
FileOutputStream fos = new FileOutputStream(destpath + file);
int tmp = -1;
while ((tmp = zis.read()) != -1) {
fos.write(tmp);
}
zis.closeEntry();
fos.close();
}
}
zis.close();
}

/**
* 压缩目录
* zipFileName:压缩后zip文件的路径
* inputFile:需要压缩的源目录
* */
public void zip(String zipFileName,String inputFile)throws Exception{
File file =new File(inputFile);
ZipOutputStream out =new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out,file,"");
System.out.println("zip done");
out.close();

}
//递归遍历子目录和文件,进行压缩
private void zip(ZipOutputStream out, File f, String base) throws Exception{
System.out.println("Zipping"+f.getName());
if(f.isDirectory()){
File[] fl=f.listFiles();
out.putNextEntry(new ZipEntry(base+"/"));
base = base.length()==0?"":base+"/";
for(int i=0;i<fl.length;i++){
zip(out,fl[i],base+fl[i].getName());
}
}else{
out.putNextEntry(new ZipEntry(base));
FileInputStream in =new FileInputStream(f);
int b;
while((b=in.read())!=-1){
out.write(b);
in.close();
}
}
}

/**
* 解压缩目录
* zipFileName:需要解压缩的zip文件
* outputDirectory:解压缩后文件所放路径
* */
public void unzip2(String zipFileName,String outputDirectory) throws Exception{
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while((z=in.getNextEntry())!=null){
System.out.println("unziping:"+z.getName());
if(z.isDirectory()){
String name=z.getName();
name=name.substring(0,name.length()-1);
File f=new File(outputDirectory+File.separator+name);
f.mkdirs();
System.out.println("mkdirs:"+outputDirectory+File.separator+name);
}else{
File f=new File(outputDirectory+File.separator+z.getName());
f.createNewFile();
FileOutputStream out=new FileOutputStream(f);
int b;
while((b=in.read())!=-1){
out.write(b);
}
out.close();
}
}
in.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值