Java 压缩zip格式文件

三个主要的类:

  • import java.util.zip.ZipFile;//打开zip格式文件。
  • import java.util.zip.ZipEntry; //标记被压缩文件的属性,主要是文件名和路径。
  • import java.util.zip.ZipOutputStream;//zip格式输出流。

压缩文件夹时,需要挨个结点,递归压缩文件夹中所有的文件。

	public static void zipFile(String filePath, String zipPath) {
		try {
			File inFile = new File(filePath);
			File outFile = new File(zipPath);
			FileOutputStream fos = new FileOutputStream(outFile);
			ZipOutputStream zos = new ZipOutputStream(fos);
			zipFile(filePath, inFile, zos);
			zos.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 压缩文件夹
	 * @date 2016年4月13日 下午7:47:11
	 * @param folderPath
	 * @param zipPath
	 */
	public static void zipFolder(String folderPath, String zipPath) {

		try {
			File inFile = new File(folderPath);
			File outFile = new File(zipPath);
			FileOutputStream fos = new FileOutputStream(outFile);
			ZipOutputStream zos = new ZipOutputStream(fos);
			zipFile(folderPath, inFile, zos);
			zos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 递归压缩文件
	 * @date 2016年4月13日 下午7:47:28
	 * @param folderPath
	 * @param file
	 * @param zos
	 */
	private static void zipFile(String folderPath, File file, ZipOutputStream zos) {

		int index = folderPath.lastIndexOf(File.separator);
		
		try {
			if (file.isDirectory()) {// 文件夹,递归遍历文件夹下文件压缩
				File[] files = file.listFiles();
				if (files != null) {
					for (File f : files)
						zipFile(folderPath, f, zos);
				}
				return;
			}
			zos.putNextEntry(new ZipEntry(file.getAbsolutePath().substring(index)));
			byte[] buffer = new byte[1024];
			int length = -1;
			FileInputStream fis = new FileInputStream(file);
			while ((length = fis.read(buffer)) != -1) {
				zos.write(buffer, 0, length);
			}
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 解压文件.zip格式
	 * 
	 * @date 2016年4月13日 下午3:12:23
	 * @param zipFilePath
	 * @param outFolderPath
	 */
	public static void unzipFile(String zipFilePath, String outFolderPath) {

		try {
			ZipFile zipFile = new ZipFile(zipFilePath);

			Enumeration<ZipEntry> zList = (Enumeration<ZipEntry>) zipFile.entries();
			while (zList.hasMoreElements()) {
				ZipEntry zipEntry = zList.nextElement();
				if (zipEntry.getName().startsWith("__MACOSX"))// Mac
																// 系统自带压缩工具生成,屏蔽
					continue;
				if (!zipEntry.isDirectory()) {// 只解压文件
					InputStream is = zipFile.getInputStream(zipEntry);
					StringBuffer name = new StringBuffer(outFolderPath);
					if (name.lastIndexOf(File.separator) != name.length() - 1) {
						name.append(File.separator);
					}
					name.append(zipEntry.getName());
					File outFile = new File(name.toString());
					if (!outFile.getParentFile().exists())
						outFile.getParentFile().mkdirs();
					outFile.createNewFile();
					FileOutputStream fos = new FileOutputStream(outFile);
					int length = -1;
					byte[] buffer = new byte[1024];
					while ((length = is.read(buffer)) != -1) {
						fos.write(buffer, 0, length);
					}
					// 关闭流
					is.close();
					fos.close();
				}

			}
			zipFile.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
如果使用 Java 压缩 ZIP 文件时出现损坏的问题,可能是由于以下原因导致的: 1. 压缩文件路径或文件名包含特殊字符,例如中文字符、空格、特殊符号等,这可能会导致文件路径或文件名无法正确解析,从而导致压缩文件损坏。 2. 压缩文件中包含非法字符或不兼容的文件格式,例如压缩文件中包含了损坏的文件或不支持的文件格式,这些都可能导致压缩文件损坏。 3. 压缩算法或压缩级别不正确,如果压缩算法或压缩级别设置不正确,也可能导致压缩文件损坏。 下面是一些可能有用的建议来解决这些问题: 1. 避免在压缩文件路径和文件名中使用特殊字符,例如中文字符、空格和特殊符号。可以尝试使用英文字母和数字来命名文件和路径。 2. 确保要压缩文件都是合法的,并且文件格式兼容。可以尝试对要压缩文件进行检查,确保它们没有损坏并且可以被正确读取。 3. 确认使用的压缩算法和级别,确保它们是合适的。例如,使用较高的压缩级别可能导致压缩文件变得更小,但也可能导致压缩时间变长,甚至可能导致压缩文件损坏。可以尝试使用默认的压缩算法和级别,以避免这些问题。 下面是一个示例代码来演示如何使用 Java 压缩 ZIP 文件: ```java import java.io.*; import java.util.zip.*; public class ZipCompressor { public static void compress(File sourceFile, File zipFile) throws IOException { // 创建 ZIP 输出流 FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); // 压缩文件 compressFile(sourceFile, zos, ""); // 关闭 ZIP 输出流 zos.close(); fos.close(); } private static void compressFile(File file, ZipOutputStream zos, String path) throws IOException { if (file.isDirectory()) { // 压缩目录 File[] files = file.listFiles(); if (files != null && files.length > 0) { for (File subFile : files) { compressFile(subFile, zos, path + file.getName() + "/"); } } } else { // 压缩文件 String entryPath = path + file.getName(); ZipEntry entry = new ZipEntry(entryPath); zos.putNextEntry(entry); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } } ``` 在上面的示例代码中,我们使用 ZipOutputStream 类来创建 ZIP 输出流,并使用 compressFile() 方法来递归地压缩目录和文件。需要注意的是,在压缩文件时,我们需要确保文件名和路径不包含特殊字符,并且压缩算法和级别设置正确。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值