`
yunzhu
  • 浏览: 1144315 次
  • 性别: Icon_minigender_1
  • 来自: 南京
博客专栏
B2b19957-cda7-3a9e-83a0-418743feb0ca
监控应用服务器
浏览量:109693
2e8be8be-e51f-346c-bcdd-12623c9aa820
Web前端开发
浏览量:119655
Bfa5df64-a623-34b9-85b8-ef3ce2aed758
经典异常的解决
浏览量:204456
社区版块
存档分类
最新评论

Zip压缩工具类(基于JDK和基于ant.jar)

阅读更多

基于JDK的Zip压缩工具类

该版本存在问题:压缩时如果目录或文件名含有中文,压缩后会变成乱码

package cn.chenfeng.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;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * 基于JDK的Zip压缩工具类
 * 
 * <pre>
 * 存在问题:压缩时如果目录或文件名含有中文,压缩后会变成乱码
 * </pre>
 * 
 * @author 陈峰
 */
public class JdkZipUtils {

	public static final int BUFFER_SIZE_DIFAULT = 128;

	public static void makeZip(String[] inFilePaths, String zipFilePath)
			throws Exception {
		File[] inFiles = new File[inFilePaths.length];
		for (int i = 0; i < inFilePaths.length; i++) {
			inFiles[i] = new File(inFilePaths[i]);
		}
		makeZip(inFiles, zipFilePath);
	}

	public static void makeZip(File[] inFiles, String zipFilePath)
			throws Exception {
		ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(zipFilePath)));
		for (int i = 0; i < inFiles.length; i++) {
			doZipFile(zipOut, inFiles[i], inFiles[i].getParent());
		}
		zipOut.flush();
		zipOut.close();
	}

	private static void doZipFile(ZipOutputStream zipOut, File file,
			String dirPath) throws FileNotFoundException, IOException {
		if (file.isFile()) {
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(file));
			String zipName = file.getPath().substring(dirPath.length());
			while (zipName.charAt(0) == '\\' || zipName.charAt(0) == '/') {
				zipName = zipName.substring(1);
			}
			ZipEntry entry = new ZipEntry(zipName);
			zipOut.putNextEntry(entry);
			byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
			int size;
			while ((size = bis.read(buff, 0, buff.length)) != -1) {
				zipOut.write(buff, 0, size);
			}
			zipOut.closeEntry();
			bis.close();
		} else {
			File[] subFiles = file.listFiles();
			for (File subFile : subFiles) {
				doZipFile(zipOut, subFile, dirPath);
			}
		}
	}

	public static void unZip(String zipFilePath, String storePath)
			throws IOException {
		unZip(new File(zipFilePath), storePath);
	}

	public static void unZip(File zipFile, String storePath) throws IOException {
		if (new File(storePath).exists()) {
			new File(storePath).delete();
		}
		new File(storePath).mkdirs();

		ZipFile zip = new ZipFile(zipFile);
		Enumeration<? extends ZipEntry> entries = zip.entries();
		while (entries.hasMoreElements()) {
			ZipEntry zipEntry = entries.nextElement();

			if (zipEntry.isDirectory()) {
				// TODO
			} else {
				String zipEntryName = zipEntry.getName();
				if (zipEntryName.indexOf(File.separator) > 0) {
					String zipEntryDir = zipEntryName.substring(0, zipEntryName
							.lastIndexOf(File.separator) + 1);
					String unzipFileDir = storePath + File.separator
							+ zipEntryDir;
					File unzipFileDirFile = new File(unzipFileDir);
					if (!unzipFileDirFile.exists()) {
						unzipFileDirFile.mkdirs();
					}
				}

				InputStream is = zip.getInputStream(zipEntry);
				FileOutputStream fos = new FileOutputStream(new File(storePath
						+ File.separator + zipEntryName));
				byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
				int size;
				while ((size = is.read(buff)) > 0) {
					fos.write(buff, 0, size);
				}
				fos.flush();
				fos.close();
				is.close();
			}
		}
	}

	public static void main(String[] args) throws Exception {
		String rootDir = "D:\\chenfeng";
		File[] inFiles = new File(rootDir).listFiles();
		String zipPath = "D:\\ZipDemo.zip";

		makeZip(inFiles, zipPath);

		unZip(zipPath, "D:\\chenfeng_zip");
	}
}

 

基于Ant的Zip压缩工具类

解决了上述基于JDK的工具类所存在的乱码问题

需要第三方JAR包:Apache的ant.jar,见附件

package cn.chenfeng.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;
import java.io.InputStream;
import java.util.Enumeration;

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

/**
 * 基于Ant的Zip压缩工具类
 * 
 * @author 陈峰
 */
public class AntZipUtils {

	public static final String ENCODING_DEFAULT = "UTF-8";

	public static final int BUFFER_SIZE_DIFAULT = 128;

	public static void makeZip(String[] inFilePaths, String zipPath)
			throws Exception {
		makeZip(inFilePaths, zipPath, ENCODING_DEFAULT);
	}

	public static void makeZip(String[] inFilePaths, String zipPath,
			String encoding) throws Exception {
		File[] inFiles = new File[inFilePaths.length];
		for (int i = 0; i < inFilePaths.length; i++) {
			inFiles[i] = new File(inFilePaths[i]);
		}
		makeZip(inFiles, zipPath, encoding);
	}

	public static void makeZip(File[] inFiles, String zipPath) throws Exception {
		makeZip(inFiles, zipPath, ENCODING_DEFAULT);
	}

	public static void makeZip(File[] inFiles, String zipPath, String encoding)
			throws Exception {
		ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(zipPath)));
		zipOut.setEncoding(encoding);
		for (int i = 0; i < inFiles.length; i++) {
			File file = inFiles[i];
			doZipFile(zipOut, file, file.getParent());
		}
		zipOut.flush();
		zipOut.close();
	}

	private static void doZipFile(ZipOutputStream zipOut, File file,
			String dirPath) throws FileNotFoundException, IOException {
		if (file.isFile()) {
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(file));
			String zipName = file.getPath().substring(dirPath.length());
			while (zipName.charAt(0) == '\\' || zipName.charAt(0) == '/') {
				zipName = zipName.substring(1);
			}
			ZipEntry entry = new ZipEntry(zipName);
			zipOut.putNextEntry(entry);
			byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
			int size;
			while ((size = bis.read(buff, 0, buff.length)) != -1) {
				zipOut.write(buff, 0, size);
			}
			zipOut.closeEntry();
			bis.close();
		} else {
			File[] subFiles = file.listFiles();
			for (File subFile : subFiles) {
				doZipFile(zipOut, subFile, dirPath);
			}
		}
	}

	public static void unZip(String zipFilePath, String storePath)
			throws IOException {
		unZip(new File(zipFilePath), storePath);
	}

	public static void unZip(File zipFile, String storePath) throws IOException {
		if (new File(storePath).exists()) {
			new File(storePath).delete();
		}
		new File(storePath).mkdirs();

		ZipFile zip = new ZipFile(zipFile);
		Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip
				.getEntries();
		while (entries.hasMoreElements()) {
			ZipEntry zipEntry = entries.nextElement();
			if (zipEntry.isDirectory()) {
				// TODO
			} else {
				String zipEntryName = zipEntry.getName();
				if (zipEntryName.indexOf(File.separator) > 0) {
					String zipEntryDir = zipEntryName.substring(0, zipEntryName
							.lastIndexOf(File.separator) + 1);
					String unzipFileDir = storePath + File.separator
							+ zipEntryDir;
					File unzipFileDirFile = new File(unzipFileDir);
					if (!unzipFileDirFile.exists()) {
						unzipFileDirFile.mkdirs();
					}
				}

				InputStream is = zip.getInputStream(zipEntry);
				FileOutputStream fos = new FileOutputStream(new File(storePath
						+ File.separator + zipEntryName));
				byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
				int size;
				while ((size = is.read(buff)) > 0) {
					fos.write(buff, 0, size);
				}
				fos.flush();
				fos.close();
				is.close();
			}
		}
	}

	public static void main(String[] args) throws Exception {
		String rootDir = "D:\\chenfeng";
		String zipPath = "D:\\ZipDemo.zip";
		// File[] inFiles = new File(rootDir).listFiles();
		// makeZip(inFiles, zipPath);
		makeZip(new String[] { rootDir }, zipPath);

		unZip(zipPath, "D:\\chenfeng_zip");
	}
}

 

 

 

 

  • ant.jar (1.8 MB)
  • 下载次数: 101
0
0
分享到:
评论
1 楼 987812 2016-07-31  
JDK 自带的zip 压缩工具 要设置编码

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
不过这种方式非常慢,压缩率也非常低,ant 很快。不知道怎么能让压缩率高点。100M 的文件 压缩后大小变化不大!

相关推荐

    ant-1.6.5.jar.zip

    `ant-1.6.5.jar`是Ant 1.6.5的主库,包含了所有执行构建任务所需的类和资源。这个JAR文件是使用Java编程语言编写的,可以被任何Java环境运行。用户可以通过执行`java -jar ant-1.6.5.jar`命令来启动Ant,然后结合`...

    ant-nodeps.jar.zip

    标题“ant-nodeps.jar.zip”指的是一个压缩文件,其中包含了Ant构建工具的一个特定版本,去除了外部依赖的jar包。这个文件的目的是为了方便开发者在没有其他额外库的情况下使用Ant。Ant是一个Java编写的开源构建工具...

    ant-ikvmc.0.5.jar.zip

    标题中的"ant-ikvmc.0.5.jar.zip"是一个压缩文件,它包含了与Ant构建工具相关的IKVMC工具的0.5版本。Ant是一个广泛使用的Java项目自动化构建工具,它基于XML来定义任务,使得编译、打包、测试等步骤能够自动化执行。...

    tools and ant jar

    首先,"tools.jar" 是Java开发工具集的一部分,它包含了Java编译器(javac)、Java文档生成器(javadoc)和其他一些开发工具的类。这个库是Java SDK的标准组件,通常位于JDK安装目录的`lib`子目录下。当你在Java项目...

    java-unrar-0.5.jar、commons-logging-1.2.jar和ant-1.6.5.jar

    总之,`java-unrar-0.5.jar`、`commons-logging-1.2.jar`和`ant-1.6.5.jar`这三个库在Java环境中为解压ZIP和RAR文件提供了全面的工具集。它们分别解决了RAR文件操作、日志管理和自动化构建的需求,使得开发者能够更...

    ant-1.5.2.jar.zip

    总结来说,"ant-1.5.2.jar.zip"是Ant 1.5.2版本的压缩包,包含Ant的主程序和许可协议信息。通过理解和使用Ant,开发者可以高效地自动化Java项目的构建过程,提高开发效率,并确保构建的一致性和可重复性。对于Java...

    ant-commons-logging-1.6.5.jar.zip

    总的来说,"ant-commons-logging-1.6.5.jar.zip"提供了在Apache Ant项目中进行日志记录的灵活性和可扩展性,使得开发者可以选择最适合他们项目的日志框架。这个压缩包的使用不仅简化了日志处理,还增强了项目的可...

    解压zip压缩文件,支持多文件目录解压,中文乱码问题

    在使用Java对ZIP压缩文件进行解压的方式中有两种,一种是使用apache提供的ant.jar工具包,但是如果ZIP文件中含有中文名称的文件,在解压后,文件名将出现乱码,另一种是使用Java自身JDK中java.util.zip包下工具类,...

    bcprov-jdk15-138.zip

    总的来说,"bcprov-jdk15-138.zip" 包含了Bouncy Castle的Java 1.5兼容版本的源代码,为开发者提供了强大的加密工具集,能够增强Java应用程序的安全性和功能性。通过深入研究这些源代码,开发者不仅可以学习到加密...

    java压缩zip文件解决中文乱码问题

    Ant是一个强大的构建工具,它包含了处理文件操作,包括压缩和解压的功能。例如,你可以使用Ant的`&lt;zip&gt;`任务来创建ZIP文件,通过指定`encoding`属性来解决中文乱码问题: ```xml &lt;zip destfile="output.zip" ...

    apache-ant-1.9.6

    Apache Ant 是一个开源的构建工具,广泛用于Java项目,它基于XML来定义项目构建过程。在给定的压缩包文件“apache-ant-1.9.6”中,包含了三个子文件,分别是“apache-ant-1.9.6-bin.zip”,“apache-ant-1.9.6-...

    Windows的java.zip工具

    Java.zip工具是Windows环境下用于处理.zip格式压缩文件的实用程序,它是Java开发工具包(Java Development Kit, JDK)的一部分。这个工具集包含了多种命令行工具,可以帮助开发者进行Java应用程序的打包、解压以及...

    JDK环境变量自动配置工具v1.4.2.25.zip

    Java Development Kit(JDK)是Java编程语言的核心组件,它包含了一个Java运行时环境(JRE)、编译器(javac)以及各种开发工具,如Java应用启动器、jar打包工具等。在安装JDK后,为了能够在命令行中顺利执行Java...

    apache-ant-1.9.14.zip

    Ant是基于XML的,它的配置文件(通常命名为build.xml)定义了构建过程中的任务和依赖关系。 **Ant的基本概念** 1. **任务(Task)**:Ant的工作单元,执行特定的动作,如编译Java源代码、创建JAR文件、复制文件等。...

    proguard_v5.2.1_jdk1.8.zip

    ProGuard是一款强大的Java和Android代码优化、混淆、压缩和预校验工具,其版本为5.2.1,专门针对JDK1.8环境设计。它对于开发者来说是不可或缺的,尤其是在发布Android应用时,可以有效地减小程序体积,提高运行效率...

    Ant1.8.2 编译打包工具

    Ant是Apache软件基金会开发的一款基于Java的项目构建工具,它以XML格式定义构建过程,使得构建脚本具有可读性高、易于维护的特点。Ant 1.8.2 是该工具的一个版本,发布于2010年,旨在提供更稳定、高效的项目构建功能...

    ant使用教程.pdf

    Ant的任务(task)是构建过程中的基本操作单元,比如javac任务用于编译Java源代码,jar任务用于打包类文件到JAR文件,copy任务用于复制文件或目录等。每个任务都有自己的属性和可能的子元素,可以根据具体需求进行配置...

    9JavaJAR打包技术[收集].pdf

    例如,当`MANIFEST.MF`中设置`Main-Class: testjar.Test`时,执行`java -jar test.jar`就能直接运行`testjar.Test`类的`main`方法。 在开发过程中,我们可以通过修改`MANIFEST.MF`文件来简化JAR的执行。例如,如果...

    ant 1.9.4 打包

    Ant是Apache软件基金会的一个开放源代码项目,它是一个基于Java的构建工具,广泛用于Java项目的构建、编译、测试和部署。本篇文章将详细介绍Ant 1.9.4的安装配置过程,以及如何利用它进行打包操作。 首先,让我们...

Global site tag (gtag.js) - Google Analytics