java解压缩文件,解决中文乱码。

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;


/**
 * @author 王泽浩
 * 
 *         E-mail: 1028625100@qq.com
 * 
 * @version v1.1
 * 
 *          创建时间:2015年1月4日 上午11:18:47
 *          
 *          使用ant工具包可以解决解压缩文件中文乱码问题,需要引入ant-x.x.x.jar(项目使用的是ant-1.9.4.jar)
 */
public class Zip {

	/**
	 * 解压zip
	 * 
	 * @param path
	 *            解压到的目录("D:/test")
	 * 
	 * @param zipFile
	 * 
	 *            压缩文件(new ZipFile(new File("D:/test.zip")))
	 * 
	 * @return boolean 返回true时解压成功,false择解压失败
	 */
	public static boolean unZip(String path, ZipFile zipFile) {

		FileOutputStream fileOutputStream = null;

		InputStream inputStream = null;

		File file = null;

		try {

			int bufSize = 512;

			byte[] buf = new byte[bufSize];

			int readedBytes;

			for (Enumeration<ZipEntry> entries = zipFile.getEntries(); entries
					.hasMoreElements();) {

				ZipEntry entry = entries.nextElement();

				file = new File(path + "/" + entry.getName());

				if (entry.isDirectory()) {

					file.mkdirs();

				} else {

					File parent = file.getParentFile();

					if (!parent.exists()) {

						parent.mkdirs();

					}

					inputStream = zipFile.getInputStream(entry);

					fileOutputStream = new FileOutputStream(file);

					while ((readedBytes = inputStream.read(buf)) > 0) {

						fileOutputStream.write(buf, 0, readedBytes);

					}

					close(fileOutputStream, inputStream);

				}

			}

			zipFile.close();

			return true;

		} catch (Exception e) {

			e.printStackTrace();

			return false;

		} finally {

			close(fileOutputStream, inputStream);

		}

	}

	/**
	 * 压缩文件
	 * 
	 * @param src
	 *            将要压缩的文件夹(new File("D:/test/"))
	 * 
	 * @param dest
	 *            将压缩文件夹存储到什么位置(new File(D:/test.zip))
	 * 
	 * @return boolean 返回true时压缩成功,false择压缩失败
	 */
	public static boolean zip(File src, File dest) {

		try {

			Project prj = new Project();

			org.apache.tools.ant.taskdefs.Zip zip = new org.apache.tools.ant.taskdefs.Zip();

			zip.setProject(prj);

			zip.setDestFile(dest);

			FileSet fileSet = new FileSet();

			fileSet.setProject(prj);

			if (src.isFile()) {

				fileSet.setFile(src);

			} else {

				fileSet.setDir(src);

			}

			zip.addFileset(fileSet);

			zip.execute();

			return true;

		} catch (Exception e) {

			e.printStackTrace();

			return false;

		}

	}

	/**
	 * 关闭流
	 * 
	 * @param autoCloseables
	 */
	public static void close(AutoCloseable... autoCloseables) {

		try {

			if (autoCloseables != null) {

				for (AutoCloseable autoCloseable : autoCloseables) {

					autoCloseable.close();
				}
			}

		} catch (Exception e) {

			e.printStackTrace();

		}

	}
}








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解压缩zip文件中含有中文名的文件时,可能会出现乱码或者压失败的问题。这是因为在压缩文件时,文件名使用了默认编码格式,而在解压缩时,压软件使用了不同的编码格式,导致文件名析错误。 为了解决这个问题,可以通过指定解压缩文件名的编码格式来解决。以下是一个Java程序示例,用于解压缩zip文件并处理中文文件名的编码问题: ```java import java.io.*; import java.util.*; import java.util.zip.*; public class ZipUtils { public static void unzip(File zipFile, File destDir, String charset) throws IOException { if (!destDir.exists()) { destDir.mkdirs(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile), Charset.forName(charset)); ZipEntry entry; byte[] buffer = new byte[1024]; while ((entry = zipIn.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { File subDir = new File(destDir, fileName); subDir.mkdirs(); continue; } File file = new File(destDir, fileName); File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } OutputStream out = new FileOutputStream(file); int len; while ((len = zipIn.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); } zipIn.close(); } } ``` 在调用此方法时,可以指定解压缩文件名的编码格式,例如: ```java ZipUtils.unzip(new File("sun.zip"), new File("destDir"), "GBK"); ``` 这里的编码格式使用了GBK,根据实际情况可以修改为其他编码格式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值