java 文件和文件夹的压缩实现

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ContainerTest {
static ArrayList<String> strFileArr = new ArrayList<String>();
/**
* 打印字符串列表
*/
public static void printFilePath(ArrayList<String> strFileArr)
{
System.out.println("File path is:");
Iterator<String> it = strFileArr.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
//打印文件夹下文件路径
public static void print(File filename)
{
if(filename != null){
File[] fileArray=filename.listFiles();

for (int i = 0; i < fileArray.length; i++) {
if(!fileArray[i].isDirectory()){
System.out.println(fileArray[i].toString());
}
else{
System.out.println("Is directory :\n" + fileArray[i].toString());
print(fileArray[i]);
}
}
}
}
//压缩单个文件
public static void zipFile(String filesrc, String filedes)
{
int ilast = filedes.lastIndexOf(File.separator);
byte[] buffer = new byte[1024];
int len;
try {
FileOutputStream fos = new FileOutputStream(filedes);
ZipOutputStream zos = new ZipOutputStream(fos);
String zipfileStr = filesrc.substring(ilast+1);
ZipEntry ze= new ZipEntry(zipfileStr);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(filesrc);
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//生成文件夹下各个文件的相对路径
public static void generateFileStr(String filesrc,String srcPathDir)
{
File file = new File(filesrc);
if(file!=null){
String strTmp ="";
if(file.isDirectory()){
File[] fileArray=file.listFiles();
for (int i = 0; i < fileArray.length; i++){
strTmp = fileArray[i].toString();
if(!fileArray[i].isDirectory()){
strFileArr.add(strTmp.substring(srcPathDir.length()+1));
System.out.println(strTmp);
}
else{
generateFileStr(strTmp,srcPathDir);
}
}
}
else{
strTmp = filesrc.substring(filesrc.length()+1);
strFileArr.add(strTmp);
System.out.println(strTmp);
}
}
}
//压缩文件夹
public static void zipDir(String filesrc, String filedes)
{
System.out.println("zipDir");
File file = new File(filesrc);

if(file.isDirectory()){
generateFileStr(filesrc , filesrc);
byte[] buffer = new byte[1024];
int len;
try {
String filepath = null;
FileOutputStream fos = new FileOutputStream(filedes);
ZipOutputStream zos = new ZipOutputStream(fos);
for(int i = 0; i< strFileArr.size(); i++){
filepath = filesrc + File.separator + strFileArr.get(i);
System.out.println(filepath);
String zipfileStr =strFileArr.get(i);
System.out.println("zipDir zipfileStr :"+zipfileStr);
ZipEntry ze= new ZipEntry(zipfileStr);
zos.putNextEntry(ze);
File fileitem = new File(filepath);
System.out.println("zipDir filepath :"+filepath);
if(fileitem.isFile()){
FileInputStream in = new FileInputStream(fileitem);
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
else{

}
}
zos.closeEntry();
zos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String zipFile = new String("D:" +  File.separator + "zipFile.zip");
String srcfile = new String("E:" + File.separator +"test" );
zipDir(srcfile, zipFile);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值