小鱼仔做文件压缩解压的时候,开始并没有注意细节,使用的java jdk中zip操作工具类
这样导致的问题是对中文操作都是乱码,要么就是破损。
查阅资料才知道这个实在1.6的历史遗留问题
还好开源的apache 提供了一个支持包,让我们拿过来就可以用。
下面是下载链接
提供jar包下载链接:http://download.csdn.net/detail/u010962482/8748091
废话不多说 贴上解压压缩代码。
/**
* 解压文件到指定目录
* @param zipFile
* @param descDir
* @author isea533
*/
@SuppressWarnings( "rawtypes")
public static void unZipFiles(String zip_s_File,String descDir){
OutputStream out= null;
InputStream in = null;
File zipFile= new File(zip_s_File);
//判断解压路径是否存在,不存在则创建
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
//创建待解压文件夹路径
ZipFile zip;
try {
<span style="white-space:pre"> </span>//注意 这里的编码 按自己情况自己填写
zip = new ZipFile(zipFile,"gbk" );
for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
in = zip.getInputStream(entry);
String outPath = (descDir+zipEntryName).replaceAll("\\*" , "/" );;
//输出文件路径信息 ,如果存在文件则覆盖,如果不想覆盖修改OUTputStream的Boolean类型
System. out.println(outPath);
out = new FileOutputStream(outPath,true );
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
}
System. out.println("******************解压完毕********************" );
} catch (IOException e) {
System. out.println("解压文件失败" );
e.printStackTrace();
} finally{
try {
in.close();
out.close();
} catch (Exception e2) {
System. out.println("管理io流失败" );
e2.printStackTrace();
}
}
}
/**
*
* @param sourceFilePath 需要压缩 zip 路径
* @param zipFilePath zip压缩文件路径
* @param fileName zip 名字
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false ;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
System. out.println("待压缩的文件目录:" +sourceFilePath+"不存在." );
} else{
File zipFile = new File(zipFilePath + "/" + fileName +".zip" );
try { if(zipFile.exists()){
System. out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" + "打包文件." );
} else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
System. out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩." );
} else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
zos.setEncoding( "gbk");
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length ;i++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}
/**
* 解压文件
* @param zip_s_File 需要解压Zip文件
* @param descDir 解压文件存放路径
* @return
*/
public static boolean unzip (String zip_s_File , String descDir) {
OutputStream out= null;
InputStream in = null;
File zipFile= new File(zip_s_File) ;
//判断解压路径是否存在,不存在则创建
File pathFile = new File(descDir) ;
if (!pathFile.exists()){
pathFile.mkdirs() ;
}
//创建待解压文件夹路径
ZipFile zip ;
try {
zip = new ZipFile(zipFile , "gbk" ) ;
for (Enumeration entries = zip.getEntries() ; entries.hasMoreElements() ; ){
ZipEntry entry = (ZipEntry)entries.nextElement() ;
String zipEntryName = entry.getName() ;
in = zip.getInputStream(entry) ;
String outPath = (descDir+zipEntryName).replaceAll( " \\ *" , "/" ) ;
//输出文件路径信息 ,如果存在文件则覆盖,如果不想覆盖修改OUTputStream的Boolean类型
out = new FileOutputStream(outPath ,true ) ;
byte [] buf1 = new byte [ 1024 ] ;
int len ;
while ((len=in.read(buf1))> 0 ){
out.write(buf1 , 0 , len) ;
}
out.flush() ;
}
System. out .println( "******************解压完毕********************" ) ;
return true;
} catch (IOException e) {
System. out .println( "解压文件失败" ) ;
e.printStackTrace() ;
return false;
} finally {
try {
if (in!= null ){
in.close() ;
} if (out!= null ){
out.close() ;
}
} catch (Exception e2) {
System. out .println( "管理io流失败" ) ;
e2.printStackTrace() ;
}
}
}