java完成zip压缩

1、java.util.zip的方式(为了解决文件名的中文问题需要修改源码):


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Compress_Java {
	
	//文件压缩
	public void zip(String zipName, String filePath){
		try {
			FileOutputStream fos = new FileOutputStream(zipName);
			ZipOutputStream out = new ZipOutputStream(fos, "GBK");
			File inputFile = new File(filePath);
		    compress(out, inputFile, null);  
		    System.out.println("压缩完成"); 
		    out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//压缩逻辑
	public void compress(ZipOutputStream zos, File file, String base){
		//遍历路径下所有文件进行压缩
		byte[] b = new byte[1024];
		FileInputStream fis = null;
		//处理目录和文件
		if(file.isDirectory()){
			base = base == null ? file.getName() : base  + "/" + file.getName();
			try {
				zos.putNextEntry(new ZipEntry(base + "/"));
				File[] fileList = file.listFiles();
				for(int i = 0 ; i < fileList.length ; i++)
				{
					if(fileList[i].isDirectory())
					{
						compress(zos, fileList[i], base);
					}
					else 
					{	
						zos.putNextEntry(new ZipEntry(base + "/" +  fileList[i].getName()));//这个一定不要忘记了,不然文件是压缩不进入的哦
						fis = new FileInputStream(fileList[i]);
						while(fis.read(b) != -1)
						{
							zos.write(b);
						}
						fis.close();
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			base = base == null ? file.getName() : base  + "/" + file.getName();
			try {
				zos.putNextEntry(new ZipEntry(base));
				fis = new FileInputStream(file);
				while(fis.read(b) != -1)
				{
					zos.write(b);
				}
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Compress_Java c = new Compress_Java();
		c.zip("F:\\ZipTest.zip", "F:\\我的资料");
	}

}
修改后的源码会上传到csdn上。

2、apache的方式


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


public class Compress_Apache {

	public void zip(String zipName, String filePath){
		try {
			FileOutputStream fos = new FileOutputStream(zipName);
			ZipOutputStream out = new ZipOutputStream(fos);
			File inputFile = new File(filePath);
		    compress(out, inputFile, null);  
		    System.out.println("压缩完成"); 
		    out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void compress(ZipOutputStream zos, File file, String base){
		//遍历路径下所有文件进行压缩
		byte[] b = new byte[1024];
		FileInputStream fis = null;
		//处理目录和文件
		if(file.isDirectory()){
			base = base == null ? file.getName() : base  + "/" + file.getName();
			try {
				zos.putNextEntry(new ZipEntry(base + "/"));
				File[] fileList = file.listFiles();
				for(int i = 0 ; i < fileList.length ; i++)
				{
					if(fileList[i].isDirectory())
					{
						compress(zos, fileList[i], base);
					}
					else 
					{	
						zos.putNextEntry(new ZipEntry(base + "/" +  fileList[i].getName()));
						fis = new FileInputStream(fileList[i]);
						while(fis.read(b) != -1)
						{
							zos.write(b);
						}
						fis.close();
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			base = base == null ? file.getName() : base  + "/" + file.getName();
			try {
				zos.putNextEntry(new ZipEntry(base));
				fis = new FileInputStream(file);
				while(fis.read(b) != -1)
				{
					zos.write(b);
				}
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}	
		
	}
	
	
	public static void main(String[] args) {
		Compress_Apache c = new Compress_Apache();
		c.zip("F:\\ZipTT.zip", "F:\\我的资料");
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值