java中生成zip包

开发java项目时,可以需求传送文件,但是如果压缩一下,就太好。还好,java中有zip压缩功能,直接上例子:

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 TestZip {

	private void zip(String souceFileName, String destFileName) {
		File file = new File(souceFileName);
		try {
			zip(file, destFileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void zip(File souceFile, String destFileName) throws IOException {
		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(destFileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		ZipOutputStream out = new ZipOutputStream(fileOut);
		zip(souceFile, out, "");
		out.close();
	}

	private void zip(File souceFile, ZipOutputStream out, String base)
			throws IOException {

		if (souceFile.isDirectory()) {
			File[] files = souceFile.listFiles();
			out.putNextEntry(new ZipEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			for (File file : files) {
				zip(file, out, base + file.getName());
			}
		} else {
			if (base.length() > 0) {
				out.putNextEntry(new ZipEntry(base));
			} else {
				out.putNextEntry(new ZipEntry(souceFile.getName()));
			}
			System.out.println("filepath=" + souceFile.getPath());
			FileInputStream in = new FileInputStream(souceFile);

			int b;
			byte[] by = new byte[1024];
			while ((b = in.read(by)) != -1) {
				out.write(by, 0, b);
			}
			in.close();
		}
	}

	/**
	 * Test
	 */
	public static void main(String[] args) {
		TestZip z = new TestZip();
		z.zip("D:\\var\\wt", "D:/Data.zip");
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!如果您想要在Java实现可研导出zip的功能,您可以使用JavaZipOutputStream类来完成。下面是一个简单的示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipExporter { public static void main(String[] args) { String sourceFolder = "path/to/source/folder"; // 源文件夹路径 String zipFilePath = "path/to/output/zip/file.zip"; // 输出zip文件路径 try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); // 递归压缩文件夹的文件 File folder = new File(sourceFolder); addFilesToZip(folder, folder.getName(), zos); zos.close(); fos.close(); System.out.println("Zip文件生成成功!"); } catch (IOException e) { e.printStackTrace(); } } private static void addFilesToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException { for (File file : folder.listFiles()) { if (file.isDirectory()) { addFilesToZip(file, parentFolder + "/" + file.getName(), zos); continue; } FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(parentFolder + "/" + file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } } ``` 在上面的示例代码,您需要将`sourceFolder`变量设置为您要压缩的源文件夹的路径,将`zipFilePath`变量设置为您要生成zip文件的路径。然后,代码将会递归地压缩源文件夹的所有文件和子文件夹。 请注意,这只是一个简单的示例代码,您可能需要根据您的具体需求进行适当的修改和扩展。希望对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值