java解压缩问题 如果使用默认java自带的类
java.util.zip.ZipOutputStream 则会出现压缩后不能解压的问题
经过排查发现当压缩文件路径含有奇数个中文路径时 解压会出现问题 偶数不会有问题 这个主要是由于字符编码的问题导致的
java中gbk编码使用2字节 utf-8使用三个字节 当gbk转换到utf-8格式的时候 会扩展字节 当奇数个汉字时 扩展字节最后一位会出错
package com.fh.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class FileZip {
/**
* @param inputFileName 你要压缩的文件夹(整个完整路径)
* @param zipFileName 压缩后的文件(整个完整路径)
*/
public static void zip(String inputFileName, String zipFileName) throws Exception {
zip(zipFileName, new File(inputFileName));
}
private static void zip(String zipFileName, File inputFile) throws Exception
{
FileOutputStream f = new FileOutputStream(zipFileName);
//使用指定校验和创建输出流
CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
ZipOutputStream out = new ZipOutputStream(csum);
//支持中文
out.setEncoding("GBK");
//设置压缩包注释
out.setComment("生成的代码");
BufferedOutputStream bos = new BufferedOutputStream(out);
//启用压缩
out.setMethod(ZipOutputStream.DEFLATED);
//压缩级别为最强压缩,但时间要花得多一点
out.setLevel(Deflater.BEST_COMPRESSION);
//ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out,bos, inputFile, "");
bos.close();
out.flush();
out.close();
}
private static void zip(ZipOutputStream out,BufferedOutputStream bos, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, bos, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
//System.out.println(base);
while ((b = in.read()) != -1) {
bos.write(b);
}
bos.flush();
in.close();
}
}
public static void main(String [] temp){
try {
zip("E:\\zdw","E:\\test.zip");//你要压缩的文件夹 和 压缩后的文件
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
参考链接
https://blog.csdn.net/beyondlpf/article/details/7519786
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class FileZip {
/**
* @param inputFileName 你要压缩的文件夹(整个完整路径)
* @param zipFileName 压缩后的文件(整个完整路径)
*/
public static void zip(String inputFileName, String zipFileName) throws Exception {
zip(zipFileName, new File(inputFileName));
}
private static void zip(String zipFileName, File inputFile) throws Exception
{
FileOutputStream f = new FileOutputStream(zipFileName);
//使用指定校验和创建输出流
CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
ZipOutputStream out = new ZipOutputStream(csum);
//支持中文
out.setEncoding("GBK");
//设置压缩包注释
out.setComment("生成的代码");
BufferedOutputStream bos = new BufferedOutputStream(out);
//启用压缩
out.setMethod(ZipOutputStream.DEFLATED);
//压缩级别为最强压缩,但时间要花得多一点
out.setLevel(Deflater.BEST_COMPRESSION);
//ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out,bos, inputFile, "");
bos.close();
out.flush();
out.close();
}
private static void zip(ZipOutputStream out,BufferedOutputStream bos, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, bos, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
//System.out.println(base);
while ((b = in.read()) != -1) {
bos.write(b);
}
bos.flush();
in.close();
}
}
public static void main(String [] temp){
try {
zip("E:\\zdw","E:\\test.zip");//你要压缩的文件夹 和 压缩后的文件
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
参考链接
https://blog.csdn.net/beyondlpf/article/details/7519786