java zip压缩和解压

package com.pgp.myzip;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class UnZip {

public static void main(String[] args) {

String zipPath = "C:/Users/hucong/Desktop/assets.zip";
String saveDir = "C:/Users/hucong/Desktop/assets";

unZipFile(zipPath, saveDir);
}


private static void unZipFile(String zipPath, String saveDir) {
ZipInputStream zis = null;
try {
zis = new ZipInputStream(new FileInputStream(zipPath));
ZipEntry ze = null;
while((ze = zis.getNextEntry()) != null){
String tm = ze.getName();
String filePath = saveDir+"/"+tm;
System.out.println(filePath);

File file = new File(filePath);
//如果当前条目对应的是一个空目录
if(filePath.endsWith("/") || filePath.endsWith("\\")){
file.mkdirs();
}else{
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = -1;
while((len = zis.read(b))!=-1){
fos.write(b, 0, len);
}
fos.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(zis!=null){
zis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}




}



package com.pgp.myzip;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class ZipFolder {


public void zipFolder(int index,File file,ZipOutputStream zos) throws IOException{
if(file.isFile()){
zipSingle(index,file, zos);
}else{
File[] fs = file.listFiles();
//如果当前文件夹是空文件夹,则为空文件夹创建条目
if(fs.length == 0){
String tm = file.getAbsolutePath().substring(index)+"/";
ZipEntry ze = new ZipEntry(tm);
zos.putNextEntry(ze);
}else{
for (File f : fs) {
zipFolder(index,f, zos);
}
}
}
}




private void zipSingle(int index,File file, ZipOutputStream zos) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
//创建当前文件的条目  D:/aaa/101.txt
String tm = file.getAbsolutePath().substring(index);
ZipEntry ze = new ZipEntry(tm);
zos.putNextEntry(ze);
byte[] b = new byte[1024];
int len = -1;
while((len = fis.read(b))!= -1){
zos.write(b, 0, len);
zos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}




public static void main(String[] args) {
// File dir = new File("C:/Users/hucong/Desktop/assets");
String dir = "C:/Users/hucong/Desktop/assets";
String zipPath = "C:/Users/hucong/Desktop/assets.zip";
zip(dir, zipPath);

}




private static void zip(String dirPath, String zipPath) {
File dir = new File(dirPath);
if(dir.exists()){
Long startTime = System.currentTimeMillis();
int index = dir.getAbsolutePath().lastIndexOf("\\")+1;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipPath));
new ZipFolder().zipFolder(index,dir, zos);
zos.finish();
System.out.println("压缩成功。。。");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(zos!=null){
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Long endTime = System.currentTimeMillis();
System.out.println("耗时:"+(endTime - startTime)+"毫秒");
}else{
System.out.println("文件出错。。。");
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值