java Io流之 ZipOutputStream实现文件夹和文件压缩

 用io流简单实现了一个压缩文件的工具类。

package com.gzh.zip;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * TODO
 *
 * @author DELL
 * @version 1.0
 * @since 2022-07-14  10:32:46
 */
public class ZipFiles {

	public static void sourceToZip(String sourcePath,String targetPath) throws IOException {

		File sourceFile = new File(sourcePath);

		File targetFile = new File(targetPath);

		if(!sourceFile.exists()) throw new RuntimeException("目标文件不存在");

		if(!targetFile.isDirectory()) throw new RuntimeException("目标路径不是一个文件夹");

		File targetZipFile = null;

		if(!sourceFile.isDirectory()){
			 targetZipFile = new File(targetPath + sourceFile.getName().substring(0, sourceFile.getName().indexOf(".")) + ".zip");
		}else {
			 targetZipFile = new File(targetPath + sourceFile.getName() + ".zip");
		}

		File[] file = sourceFile.listFiles();

		ZipOutputStream zip = null;

		try{

			zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetZipFile)));

			if(sourceFile.isFile()){
				toZip(sourceFile,zip,"");
			}else {
				fileToZip(sourceFile, zip,"");
			}
		}catch (IOException e){
			e.printStackTrace();
		}finally {
			zip.close();
		}
	}

    //文件压缩
	private static void toZip(File file,ZipOutputStream zip,String targetPath) throws IOException {

		ZipEntry zipEntry = new ZipEntry(targetPath + file.getName());

		byte[] bytes = new byte[1024];

		zip.putNextEntry(zipEntry);

		InputStream read = new FileInputStream(file);

		int ch = -1;

		while((ch = read.read(bytes)) != -1){
			zip.write(bytes, 0, ch);
		}

		read.close();
		zip.closeEntry();
	}
    //文件夹压缩
	private static void fileToZip(File file,ZipOutputStream zip,String targetPath) throws IOException {

		targetPath += file.getName() + File.separator;

		ZipEntry entry = new ZipEntry(targetPath);

		zip.putNextEntry(entry);

		zip.closeEntry();

		if(file.listFiles() == null || file.listFiles().length == 0) return;

		File[] fileLists = file.listFiles();

		for(File file1 : fileLists){
			if(!file1.isDirectory()){
				toZip(file1,zip,targetPath);
			}else {
				fileToZip(file1,zip,targetPath);
			}
		}
	}
}

测试:

public class Test {
	public static void main(String[] args) throws IOException {
		ZipFiles.sourceToZip("C:\\Users\\DELL\\Desktop\\css",
                              "C:\\Users\\DELL\\Desktop\\");
	}
}

结果:

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用JavaZipOutputStream类来压缩多个文件,然后将压缩后的内容写入ByteArrayOutputStream中。接着可以使用ByteArrayOutputStream的toByteArray()方法获取压缩后的字节数组,并将其作为响应进行下载。 示例代码如下: ``` List<File> filesToZip = Arrays.asList(new File("file1.txt"), new File("file2.txt")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); for (File file : filesToZip) { ZipEntry entry = new ZipEntry(file.getName()); zos.putNextEntry(entry); byte[] bytes = Files.readAllBytes(file.toPath()); zos.write(bytes, 0, bytes.length); zos.closeEntry(); } zos.close(); byte[] zippedBytes = baos.toByteArray(); ``` 最后使用response的方式下载 zippedBytes ### 回答2: Java提供了ZipOutputStream类用于压缩文件文件夹,ByteArrayOutputStream类用于将数据写入内存缓冲区中的字节数组。 以下是使用ZipOutputStream压缩多个文件并转换为ByteArrayOutputStream进行下载的示例代码: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipMultipleFilesToByteArrayOutputStreamExample { public static void main(String[] args) { try { // 创建ByteArrayOutputStream对象 ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 创建ZipOutputStream对象,并将其与ByteArrayOutputStream关联 ZipOutputStream zipOut = new ZipOutputStream(baos); // 需要压缩文件列表 String[] filesToCompress = {"file1.txt", "file2.txt", "file3.txt"}; // 遍历文件列表,逐个进行压缩 for (String fileName : filesToCompress) { // 创建输入流读取文件内容 FileInputStream fis = new FileInputStream(fileName); // 添加文件压缩zipOut.putNextEntry(new ZipEntry(fileName)); // 将文件内容写入ZipOutputStream byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zipOut.write(buffer, 0, length); } // 关闭当前文件的输入流 fis.close(); } // 关闭ZipOutputStream zipOut.close(); // 将ByteArrayOutputStream转换为字节数组 byte[] zipBytes = baos.toByteArray(); // 下载字节数组或保存为文件等操作 // ... } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码示例实现了将多个文件压缩为一个ZIP文件,并将压缩结果转换为ByteArrayOutputStream。你可以根据需要将压缩结果进行下载或保存为文件等操作。 ### 回答3: 在Java中,可以使用ZipOutputStream类来实现多个文件压缩,并将其转换为ByteArrayOutputStream对象进行下载。 首先,需要导入相关的Java IO库和ZipOutputStream类: ```java import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; ``` 接下来,创建一个ByteArrayOutputStream对象,用于接收ZipOutputStream的输出: ```java ByteArrayOutputStream baos = new ByteArrayOutputStream(); ``` 然后,创建一个ZipOutputStream对象,并绑定到ByteArrayOutputStream上: ```java ZipOutputStream zipOut = new ZipOutputStream(baos); ``` 接着,遍历多个文件,将每个文件逐个添加到ZipOutputStream中: ```java File file1 = new File("file1.txt"); File file2 = new File("file2.txt"); addToZip(zipOut, file1); addToZip(zipOut, file2); // 将文件添加到ZipOutputStream中的方法 private static void addToZip(ZipOutputStream zipOut, File file) throws Exception { FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } zipOut.closeEntry(); fis.close(); } ``` 压缩完成后,关闭ZipOutputStream和ByteArrayOutputStream: ```java zipOut.close(); baos.close(); ``` 最后,将ByteArrayOutputStream转换为byte数组并进行下载: ```java byte[] zipBytes = baos.toByteArray(); // 将byte数组进行下载的代码,这里省略 ``` 以上就是使用Java ZipOutputStream压缩多个文件,并将其转换为ByteArrayOutputStream进行下载的步骤。注意要逐个添加文件ZipOutputStream中,并在最后关闭相关的流对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值