IO流,ZipOutputStream对文件压缩输出

1 篇文章 0 订阅
1 篇文章 0 订阅

想要对文件进行压缩操作,这就需要用到ZipOutputStream来对文件压缩操作。首先需要指明的是:ZipOutputStream如果使用java自带的api操作需要1.7以上,否则会出现中文乱码,我测试过1.6和1.8,1.6会出现乱码,1.8则不会。听说1.7已解决这个问题,但是没实操过。如果项目中使用的是jdk1.6没办法改,这时我们就需要引进apache的ant.jar,使用它提供的ZipEntry和ZipOutputStream接口

public class ZipCompress {
	private String zipFileName;	//目的地Zip文件
	private String sourceFileName;	//源文件
	
	public ZipCompress(String zipFileName, String sourceFileName) {
		this.zipFileName = zipFileName;
		this.sourceFileName = sourceFileName;
	}
	
	public void zip() throws Exception {
		System.out.println("开始压缩...");
		
		//创建zip输出流
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));

		File sourceFile = new File(sourceFileName);
		
		//调用函数
		compress(out, sourceFile, sourceFile.getName());
		
		out.close();
		System.out.println("压缩完成!");
	}
	
	public void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
		//如果路径为目录(文件夹)
		if(sourceFile.isDirectory()) {
			//取出文件夹中的文件(或子文件夹)
			File[] flist = sourceFile.listFiles();
			
			if(flist.length==0) {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
				System.out.println(base + File.separator);
				out.putNextEntry(new ZipEntry(base + File.separator));
			} else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
				for(int i=0; i<flist.length; i++) {
					compress(out, flist[i], base+File.separator+flist[i].getName());
				}
			}
		} else {
			out.putNextEntry(new ZipEntry(base));
			FileInputStream fos = new FileInputStream(sourceFile);
	        BufferedInputStream bis = new BufferedInputStream(fos);
	        int len;
	        
	        byte[] buf = new byte[1024];
	        System.out.println(base);
	        while((len=bis.read(buf, 0, 1024)) != -1) {
	        	out.write(buf, 0, len);
	        }
	        bis.close();
	        fos.close();
		}
	}
}
public class TestZip {
	public static void main(String[] args) {
		ZipCompress zipCom = new ZipCompress("E:\\test\\压缩文件包.zip", "E:\\test\\temp");
		try {
			zipCom.zip();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
ZipOutputStream是Java中一个用于压缩文件的类。它可以将多个文件文件压缩成一个zip文件,并且可以设置压缩级别、密码等选项。 下面是一个简单的例子,展示如何使用ZipOutputStream将一个文件夹中的所有文件压缩成一个zip文件: ```java import java.io.*; import java.util.zip.*; public class ZipDirectory { public static void main(String[] args) throws IOException { String inputFolder = "/path/to/input/folder"; String outputZipFile = "/path/to/output/zipfile.zip"; // 创建输出流 FileOutputStream fos = new FileOutputStream(outputZipFile); ZipOutputStream zos = new ZipOutputStream(fos); // 遍历文件夹中的所有文件,并逐一添加到ZipOutputStream中 File folder = new File(inputFolder); for (File file : folder.listFiles()) { if (!file.isDirectory()) { addToZipFile(file, zos); } } // 关闭输出流 zos.close(); fos.close(); } private static void addToZipFile(File file, ZipOutputStream zos) throws IOException { FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); } } ``` 在上面的例子中,我们首先创建了一个ZipOutputStream,然后遍历指定的文件夹中的所有文件,并使用addToZipFile()方法将每个文件逐一添加到ZipOutputStream中。在addToZipFile()方法中,我们首先创建一个ZipEntry对象,指定要添加到zip文件中的文件名,然后读取文件的内容,并将其写入ZipOutputStream中。最后,我们关闭ZipEntry和FileInputStream。 需要注意的是,ZipOutputStream只能压缩文件,不能压缩文件夹本身。如果要压缩整个文件夹,必须遍历文件夹中的所有文件,并将它们逐一添加到ZipOutputStream中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值