干活儿遇到的问题,想找个地方记下来而已。
网上找了一下,都说要用 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;
}