java.util.zip压缩zip时中文文件名无法显示

干活儿遇到的问题,想找个地方记下来而已。

网上找了一下,都说要用 ant.jar 来支持中文,其实大可不必,在压缩的时候把文件名的编码改成 ISO-8859-1 即可。免得自己忘记,还是把代码贴上吧。

public String[] filesToZips(String password, String dirName, String zipFileName) throws IOException {
		// TODO Auto-generated method stub
		File zipDir = new File(dirName);
		String[] dirList = zipDir.list();
		byte[] readBuffer = new byte[1024];
		int byteIn = 0;
		File tempZipFile = new File(dirName + "\\temp.zip");
		if (tempZipFile.exists()) tempZipFile.delete();
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempZipFile));
//		String dateString = zipFileName.substring(0, 8);
		for (int i = 0; i < dirList.length; i++) {
			if (dirList[i].contains(".txt")) {
				File f = new File(zipDir, dirList[i]);
				FileInputStream fis = new FileInputStream(f);
				try {
					ZipEntry anEntry = new ZipEntry(new String(f.getName().getBytes(),"ISO-8859-1"));
					zos.putNextEntry(anEntry);
					byteIn = fis.read(readBuffer);
					while (byteIn != -1) {
						zos.write(readBuffer, 0, byteIn);
						byteIn = fis.read(readBuffer);
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				} finally {
					fis.close();
				}
			}
		}
		tempZipFile.createNewFile();
		zos.close();
		File zipFile = new File(dirName + "\\" + zipFileName);
		if (zipFile.exists()) zipFile.delete();
//		System.out.println("zipFileName = " + zipFile.getAbsolutePath() + "----" + zipFile.getName());
//		AesZipFileEncrypter.zipAndEncryptAll(tempZipFile, zipFile, password);
		AesZipFileEncrypter enc = new AesZipFileEncrypter(zipFile);
		enc.addAll(tempZipFile, password);
		zipFile.createNewFile();
		enc.close();
		if (tempZipFile.exists()) tempZipFile.delete();
		return dirList;
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用Zip压缩中文文件名可能会出现乱码的问题,这是因为Zip文件格式默认使用的是ASCII编码,而中文字符需要使用其他编码方式进行处理。解决这个问题的方法有两种: 1. 使用Apache Commons Compress库 可以使用Apache Commons Compress库来压缩文件,这个库支持多种编码方式,可以解决中文文件名乱码的问题。示例代码如下: ```java import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; public class ZipUtil { public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipArchiveOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); InputStream is = new FileInputStream(file); IOUtils.copy(is, zos); is.close(); zos.closeArchiveEntry(); } } } ``` 2. 使用JavaZipOutputStreamJava中自带的ZipOutputStream类也可以用于压缩文件,但是需要手动设置编码方式,示例代码如下: ```java import java.io.*; import java.nio.charset.Charset; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { private static final int BUFFER_SIZE = 2 * 1024; public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipOutputStream zos = new ZipOutputStream(fos, Charset.forName("GBK")); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); zos.closeEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); InputStream is = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } is.close(); zos.closeEntry(); } } } ``` 以上两种方法都可以解决中文文件名乱码的问题,具体使用哪一种方法可以根据实际需要选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值