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

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 的文件 压缩后大小变化不大!

相关推荐

    bcprov-ext-jdk15on-1.54.jar和bcprov-jdk15on-1.54.jar压缩文件

    bcprov-ext-jdk15on-1.54.jar和bcprov-jdk15on-1.54.jar压缩文件

    bcprov-jdk15on-154.jar--bcprov-ext-jdk15on-154.jar.zip

    bcprov-jdk15on-154.jar--bcprov-ext-jdk15on-154.jar.zip

    bcprov-jdk15on-1.54.jar bcprov-ext-jdk15on-1.54.jar下载

    1.bcprov-ext-jdk15on-1.54.jar 2.bcprov-jdk15on-1.54.jar 下载地址在:http://download.csdn.net/detail/cw_hello1/9557049 2.将下载的两个JAR文件复制到:JDK安装目录\jre\lib\ext下,例如我的就是D:\Program ...

    JDK8.0(含tools.jar和dt.jar)

    JDK8.0,含tools.jar和dt.jar,可能直接安装,里面有JRE,只要设置好环境变量,就可以进行开发。

    bcprov-ext-jdk15on-1.54.jar和bcprov-jdk15on-1.54.jar

    该压缩文件包含bcprov-ext-jdk15on-1.54.jar和bcprov-jdk15on-1.54.jar,bcprov-jdk15on-154中移除了一些加密算法,bcprov-ext-jdk15on-154中依然保留。可以解决JDK1.6 HttpClient访问https网页报的错

    bcprov-jdk15to18-1.69.jar

    bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-...

    bcprov-jdk15on-1.56.jar中文文档.zip

    bcprov-jdk15on-***.jar中文文档.zip,java,bcprov-jdk15on-***.jar,org.bouncycastle,bcprov-jdk15on,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,bouncycastle,bcprov,jdk15on,中文API文档,手册,...

    bcprov-jdk15on-1.60.jar中文文档.zip

    bcprov-jdk15on-***.jar中文文档.zip,java,bcprov-jdk15on-***.jar,org.bouncycastle,bcprov-jdk15on,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,bouncycastle,bcprov,jdk15on,中文API文档,手册,...

    jdk.tools-1.7.jar

    jdk.tools-1.7.jar,解压后放到安装目录下的jdk目录下的lib下面即可。

    bcprov-jdk15on-1.64.jar

    ECC 加密工具jar包 bcprov-jdk15on-1.64.jarbcprov-jdk15on-1.64.jarbcprov-jdk15on-1.64.jarbcprov-jdk15on-1.64.jarbcprov-jdk15on-1.64.jar

    bcprov-jdk15on-1.54.jar中文文档.zip

    bcprov-jdk15on-***.jar中文文档.zip,java,bcprov-jdk15on-***.jar,org.bouncycastle,bcprov-jdk15on,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,bouncycastle,bcprov,jdk15on,中文API文档,手册,...

    bcprov-jdk15on-1.58.jar中文文档.zip

    bcprov-jdk15on-***.jar中文文档.zip,java,bcprov-jdk15on-***.jar,org.bouncycastle,bcprov-jdk15on,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,bouncycastle,bcprov,jdk15on,中文API文档,手册,...

    bcprov-jdk16-143.jar和bcprov-jdk15-135.jar

    解决no such provider: BC 问题所需的JAR 在jdk中的jre\lib\security修改java.security文件, security....在\jre\lib\ext中添加bcprov-jdk15-135.jar的jar包 bcprov-jdk16-143.jar提供加密,解密,生成密钥对等方法

    bcprov-jdk18on-1.73.jar

    一个好用的Java jar包,这个jar包包含了很多有关密码技术的库,方便在做加密的时候进行操作,在使用Maven进行程序配置的时候不需要手动引入jar包,所以这个比较适合刚学Java的同学或者有特殊需要的朋友使用。

    bcprov-jdk15on-1.46.jar中文文档.zip

    bcprov-jdk15on-***.jar中文文档.zip,java,bcprov-jdk15on-***.jar,org.bouncycastle,bcprov-jdk15on,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,bouncycastle,bcprov,jdk15on,中文API文档,手册,...

    bcprov-jdk15on-1.67.jar中文-英文对照文档.zip

    bcprov-jdk15on-1.67.jar中文-英文对照文档

    bcprov-ext-jdk15on-1.54.jar,bcprov-jdk15on-1.54.jar

    解决java.lang.RuntimeException: Could not generate DH keypair。这个的Jar包

    bcprov-jdk15on-1.68.jar

    bcprov-jdk15on-1.68.jar

    rt.jar JDK1.8源码

    JDK动态代理生成字节码,用到了ProxyGenerator.generateProxyClass()方法,这个是rt.jar包中的方法,而安装JDK之后的src.zip没有包含。苦于找不到源码,下载了一个又报错,代码也不全,这里给大家分享一下亲测可用的...

    bcprov-jdk15on-1.65.jar

    最新版的JAVA加密算法包。 The Bouncy Castle Crypto package is a Java implementation of... This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.

Global site tag (gtag.js) - Google Analytics