日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java压缩解压缩

發布時間:2025/3/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java压缩解压缩 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需要實現一個壓縮和解壓縮工具類,用java.util的zip包來實現了一個,測試發現中文支持不是很好,需要重寫,但是網上發現apache有相關包,拿過來用

/*文件名稱:壓縮解壓縮工具類 */
/**
?* @author 崔雪峰
?* @date 2015-06-02
?* 備注:新建
?*/
package utry.util;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipInputStream;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
?* 壓縮解壓縮工具類
?*/
public class ZipUtils {


/**
* 壓縮方法
* @param 壓縮文件或文件夾路徑
* @param 壓縮文件名稱
*/
public static void zip(String srcPathName, String zipFileName){ ?
? ?File file = new File(srcPathName); ?
? ?File zipFile = new File(zipFileName); ?
? ?if (!file.exists()) throw new RuntimeException(srcPathName + "不存在!");
? ?try{
? ? FileOutputStream fileOutputStream = new FileOutputStream(zipFile); ?
? ? ? ?CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ?
? ? ? ?ZipOutputStream out = new ZipOutputStream(cos); ?
? ? ? ?Properties pro=System.getProperties();
? ? ? ?String osName=pro.getProperty("os.name");
? ? ? ?if("Linux".equals(osName)||"linux".equals(osName)){
? ? ? ? out.setEncoding("GBK");//設置文件名編碼方式
? ? ? ?}else{
? ? ? ? out.setEncoding(System.getProperty("sun.jnu.encoding"));//設置文件名編碼方式
? ? ? ?}
? ? ? ?String basedir = ""; ?
? ? ? ?compress(file, out, basedir); ?
? ? ? ?out.close(); ?
? ?}catch (Exception e){?
? ? throw new RuntimeException(e); ?
? ?} ?
}
/**
* 解壓縮
* @param zipFilePath 解壓縮文件路徑
* @param destDir 目標文件夾
*/
public static void unzip(String zipFilePath, String destDir){ ?
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); //防止文件名中有中文時出錯 ?
? ?//System.out.println(System.getProperty("sun.zip.encoding")); //ZIP編碼方式 ?
? ?//System.out.println(System.getProperty("sun.jnu.encoding")); //當前文件編碼方式 ?
? ?//System.out.println(System.getProperty("file.encoding")); //這個是當前文件內容編碼方式 ?
? ?File dir = new File(destDir); ?
? ?// create output directory if it doesn't exist ?
? ?if (!dir.exists())?
? ? dir.mkdirs(); ?
? ?FileInputStream fis; ?
? ?// buffer for read and write data to file ?
? ?byte[] buffer = new byte[1024]; ?
? ?try{ ?
? ? ?fis = new FileInputStream(zipFilePath); ?
? ? ?ZipInputStream zis = new ZipInputStream(fis); ?
? ? ?java.util.zip.ZipEntry ze = zis.getNextEntry(); ?
? ? ?while (ze != null){ ?
? ? ? ?String fileName = ze.getName(); ?
? ? ? ?File newFile = new File(destDir + File.separator + fileName); ?
? ? ? ?//System.out.println("Unzipping to " + newFile.getAbsolutePath()); ?
? ? ? ?// create directories for sub directories in zip ?
? ? ? ?new File(newFile.getParent()).mkdirs(); ?
? ? ? ?FileOutputStream fos = new FileOutputStream(newFile); ?
? ? ? ?int len; ?
? ? ? ?while ((len = zis.read(buffer)) > 0){ ?
? ? ? ? ?fos.write(buffer, 0, len); ?
? ? ? ?} ?
? ? ? ?fos.close(); ?
? ? ? ?// close this ZipEntry ?
? ? ? ?zis.closeEntry(); ?
? ? ? ?ze = zis.getNextEntry(); ?
? ? ?} ?
? ? ?// close last ZipEntry ?
? ? ?zis.closeEntry(); ?
? ? ?zis.close(); ?
? ? ?fis.close(); ?
? ?}catch (IOException e){ ?
? ? ?e.printStackTrace(); ?
? ?} ?
?}?

/**
* 壓縮執行方法
* @param file?
* @param out
* @param basedir
*/
private static void compress(File file, ZipOutputStream out, String basedir){ ?
? ?/* 判斷是目錄還是文件 */
if(file.isDirectory()){ ?
? ? ? ?// System.out.println("壓縮:" + basedir + file.getName());
compressDirectory(file, out, basedir); ?
}else{ ?
? ? ? ?// System.out.println("壓縮:" + basedir + file.getName()); ?
compressFile(file, out, basedir); ?
? ?} ?
} ?
/** 壓縮一個目錄 */
private static void compressDirectory(File dir, ZipOutputStream out, String basedir){ ?
? ?if (!dir.exists()) return;
? ?File[] files = dir.listFiles(); ?
? ?for (int i = 0; i < files.length; i++){ ?
? ? ?/* 遞歸 */ ?
? ? ?compress(files[i], out, basedir + dir.getName() + "/"); ?
? ?} ?
} ?


/** 壓縮一個文件 */ ?
private static void compressFile(File file, ZipOutputStream out, String basedir){
if (!file.exists()){
return; ?
? ?}
try{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ?
? ? ? ?ZipEntry entry = new ZipEntry(basedir + file.getName());
? ? ? ?Properties pro=System.getProperties();
? ? ? ?String osName=pro.getProperty("os.name");
? ? ? ?if("Linux".equals(osName)||"linux".equals(osName)){
? ? ? ? ?entry.setUnixMode(644);
? ? ? ?}
? ? ? ?out.putNextEntry(entry); ?
? ? ? ?int count; ?
? ? ? ?byte data[] = new byte[8192]; ?
? ? ? ?while ((count = bis.read(data, 0, 8192)) != -1){ ?
? ? ? ? ?out.write(data, 0, count); ?
? ? ? ?} ?
? ? ? ?bis.close(); ?
? ?}catch (Exception e){
? ? throw new RuntimeException(e); ?
? ?} ?
? ? }?


/**
* 測試方法
* @param args
*/
public static void main(String[] args){?
//壓縮demo
ZipUtils.zip("E:\\tomcat", "逗比.zip");
? ?//解壓縮demo
String zipFilePath = "E:\\utry\\utrycore\\逗比.zip"; ?
? ?String destDir = "E:\\解壓過后"; ?
? ?ZipUtils.unzip(zipFilePath, destDir);?
?} ?
}

應該足夠用了。linux下也ok~

對了包放在這了:http://pan.baidu.com/s/1DEldW

總結

以上是生活随笔為你收集整理的java压缩解压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。