解决读中文文件名问题,请导入Ant.jar
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* @author XL
*
*/
public class ZipUtil {
/**
* @param args
*/
public static void main(String[] args) {
zip();
}
/**
* 由于java.util.zip.ZipOutputStream有中文乱码问题,
* 所以采用org.apache.tools.zip.ZipOutputStream。
*/
public static void zip() {
String zipFileName = "C:\\ttt.zip";
File srcFileName = new File("C:\\ObjectSort繁體文件名.txt");
try {
// 创建文件输入流对象
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(srcFileName), "ISO8859_1"));
// 创建文件输出流对象
FileOutputStream f = new FileOutputStream(new File(zipFileName));
CheckedOutputStream ch = new CheckedOutputStream(f, new CRC32());
// 创建ZIP数据输出流对象
ZipOutputStream zipOut = new ZipOutputStream(
new BufferedOutputStream(ch));
// 创建指向压缩原始文件的入口
ZipEntry entry = new ZipEntry(srcFileName.getName());
zipOut.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
while ((nNumber = in.read()) != -1) {
zipOut.write(nNumber);
}
// 关闭创建的流对象
zipOut.close();
in.close();
} catch (IOException e) {
System.out.println(e);
}
}
}