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;
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值