java壓縮和解壓檔案(支援中文檔名)
參考點選開啟連結,本文中加了些註釋,是個人在學習時的理解筆記,如有錯誤歡迎指正.
Apache的zip包可解決中文檔名問題。
1、maven專案的pom.xml載入jar
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.6</version>
</dependency>
2、解壓和壓縮檔案編碼
3、測試import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class CHZipUtils { /**使用GBK編碼可以避免壓縮中文檔名亂碼*/ private static final String CHINESE_CHARSET = "GBK"; /**檔案讀取緩衝區大小*/ private static final int CACHE_SIZE = 1024; /** * 壓縮檔案 * @param sourceFolder 壓縮資料夾 * @param zipFilePath 壓縮檔案輸出路徑 */ public static void zip(String sourceFolder, String zipFilePath) { OutputStream os = null; BufferedOutputStream bos = null; ZipOutputStream zos = null; try { os = new FileOutputStream(zipFilePath); bos = new BufferedOutputStream(os); zos = new ZipOutputStream(bos); // 解決中文檔名亂碼 zos.setEncoding(CHINESE_CHARSET); File file = new File(sourceFolder); String basePath = null; if (file.isDirectory()) { basePath = file.getPath(); //將此抽象路徑名轉換為路徑名字串 } else { basePath = file.getParent(); //得到檔案路徑 } zipFile(file, basePath, zos); } catch (Exception e) { e.printStackTrace(); } finally{ try { if (zos != null) { zos.closeEntry(); zos.close(); } if (bos != null) { bos.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 遞迴壓縮檔案 * @param parentFile 需壓縮的檔案 * @param basePath 壓縮檔案所在路徑 * @param zos * @throws Exception */ private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception { File[] files = new File[0]; if (parentFile.isDirectory()) { //返回資料夾中的檔案列表 files = parentFile.listFiles(); } else { //壓縮一個檔案的情況下 files = new File[1]; files[0] = parentFile; } String pathName; InputStream is; BufferedInputStream bis; byte[] cache = new byte[CACHE_SIZE]; for (File file : files) { if (file.isDirectory()) { //資料夾 再次遍歷裡面的內容 pathName = file.getPath().substring(basePath.length() + 1) + File.separator; zos.putNextEntry(new ZipEntry(pathName)); zipFile(file, basePath, zos); } else { pathName = file.getPath().substring(basePath.length() + 1); //得到檔名 is = new FileInputStream(file); bis = new BufferedInputStream(is); zos.putNextEntry(new ZipEntry(pathName)); int nRead = 0; String content=null; //bis.read 返回-1表示已經讀到檔案尾 /*bis.read(cache, 0, CACHE_SIZE)和bis.read(cache) 都是將快取資料讀到位元組陣列中,read返回zero則是沒讀完,返回-1讀取完閉。 快取中的資料讀完之後 ,就會 釋放。*/ //將字元讀入陣列 bis.read(目的快取區,開始儲存的位元組偏移量,要讀取的最大位元組數) while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) { content+=new String(cache, 0, nRead ); zos.write(cache, 0, nRead); } bis.close(); is.close(); } } } /** * 解壓壓縮包 * @param zipFilePath 壓縮檔案路徑 * @param destDir 解壓目錄 */ public static void unZip(String zipFilePath, String destDir) { ZipFile zipFile = null; //需解壓的壓縮檔案 try { BufferedInputStream bis = null; FileOutputStream fos = null; BufferedOutputStream bos = null; zipFile = new ZipFile(zipFilePath, CHINESE_CHARSET); //以壓縮檔案中顯示順序將所有檔案返回 Enumeration<ZipEntry> zipEntries = zipFile.getEntries(); File file, parentFile; ZipEntry entry; //需解壓的物件 byte[] cache = new byte[CACHE_SIZE]; while (zipEntries.hasMoreElements()) { //壓縮包內是否包含多個元素,至少包含一個物件時返回true,否則返回false //zipEntries.nextElement()如果該列舉物件至少有一個元素可提供,則返回該列舉的下一個元素 entry = (ZipEntry) zipEntries.nextElement(); if (entry.isDirectory()) { new File(destDir + entry.getName()).mkdirs(); continue; } bis = new BufferedInputStream(zipFile.getInputStream(entry)); file = new File(destDir + entry.getName()); //建立解壓檔案 parentFile = file.getParentFile(); if (parentFile != null && (!parentFile.exists())) { parentFile.mkdirs(); } fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos, CACHE_SIZE); int readIndex = 0; //寫入解壓到的檔案中 while ((readIndex = bis.read(cache, 0, CACHE_SIZE)) != -1) { fos.write(cache, 0, readIndex); } //重新整理此緩衝的輸出流,保證資料全部都能寫出 bos.flush(); bos.close(); fos.close(); bis.close(); } } catch (IOException e) { e.printStackTrace(); }finally{ try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static void main(String[] args) throws Exception { // String sourceFolder = "E:/file/新建文字文件.txt"; // String sourceFolder = "E:/file/test/資料夾/新建文字文件.txt"; String sourceFolder = "E:/file/test/"; String zipFilePath = "E:/file/zip/壓縮檔案.zip"; // CHZipUtils.zip(sourceFolder, zipFilePath); String destDir = "E:/file/unzip/"; CHZipUtils.unZip(zipFilePath, destDir); System.out.println("********執行成功**********"); }
相關參考:http://www.cnblogs.com/blogyuan/archive/2013/05/16/3082342.html
相關推薦
java壓縮和解壓檔案(支援中文檔名)
參考點選開啟連結,本文中加了些註釋,是個人在學習時的理解筆記,如有錯誤歡迎指正. Apache的zip包可解決中文檔名問題。 1、maven專案的pom.xml載入jar <dependency> <groupId>org.apa
java 壓縮和解壓lzo檔案
1、依賴 <dependency> <groupId>org.anarres.lzo</groupId> <artifactId>lzo-core&
【轉】Java壓縮和解壓文件工具類ZipUtil
span time 其他 unzip empty del pat 列表 bis 特別提示:本人博客部分有參考網絡其他博客,但均是本人親手編寫過並驗證通過。如發現博客有錯誤,請及時提出以免誤導其他人,謝謝!歡迎轉載,但記得標明文章出處:http://www.cnblogs.
java壓縮解壓檔案
記錄一下,公司在伺服器中,需要對檔案進行壓縮,然後給使用者下載故記錄一下: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExc
C# 用GZipStream 壓縮流來壓縮和解壓檔案
最近在用c#做一些工作,其中需要把檔案進行壓縮和解壓。 有很多種方法,其中比較成熟的就是用別人的類。 可以參考部落格c#壓縮檔案 比較簡單的方式就是用流壓縮,將要寫入的資料變成位元組型陣列,直接寫入流中即可。 程式碼如下: using System
vb6使用WinRAR壓縮和解壓檔案
'[先引用Registry Access Functions library(RegObj.dll)]: Function GetWINRARPath() As String Dim myReg As New Registry, KeyFound As Boolean
使用開源庫zlib壓縮和解壓檔案
zlib,一個十分強大的開源壓縮解壓庫,應用示範廣泛,很多開源庫中都有它的存在(libpng,libzplay,ffmpeg……)。 作為普通開發者只要掌握其主要的兩個函式就足夠用了: int compress(Bytef *dest, uLongf *destLen, c
JAVA壓縮和解壓字串
轉載地址:https://www.cnblogs.com/dongzhongwei/p/5964758.html lz:叮咚^v^ /*** * 壓縮GZip * * @param data * @return */ public static by
使用Ionic.Zip.dll壓縮和解壓檔案程式碼筆記
下載 Ionic.Zip.dll 元件,新增引用,引用名稱空間 using Ionic.Zip; #region Ionic.Zip 壓縮檔案 // 壓縮方法一 p
java實現zip的壓縮和解壓(支援中文檔名)
之前一直在用java-unrar-0.3.jar來解壓rar檔案包,一直很正常,但是今天來了箇中文名字的包,類似於這樣的結構: 壓縮包.rar,這個壓縮包裡面還有個資料夾也叫壓縮包,在解壓這個壓縮包的時候出現了亂碼,研究了好久,已經解決,現與大家分享下。 原來解壓
Java ZIP壓縮和解壓縮檔案(解決中文檔名亂碼問題)
Java ZIP壓縮和解壓縮檔案(解決中文檔名亂碼問題) Java ZIP壓縮和解壓縮檔案(解決中文檔名亂碼問題) 學習了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自帶的ZipOutputStream在壓縮檔
java程式碼實現檔案或資料夾的壓縮和解壓
這裡寫了個工具類,可以實現檔案的壓縮和解壓功能。 package com.cntaiping.tpi.common.utils; import java.io.BufferedInputStream; import java.io.File; import java.io
Java解壓zip檔案(支援中文字元檔案)
昨天接到了一個解壓zip檔案的任務,所以今天在做任務之前,便寫demo試了一下, 部分思路參考自:這裡 貼上程式碼: public void Decompressing2() throws IOException { String pa
Mac下壓縮和解壓rar檔案的方法
命令列 使用工具rarosx,下載地址 - 選擇系統和版本,本文下載的是rarosx-5.4.0.tar.gz - 解壓縮:tar zxvf rarosx-5.4.0.tar.gz 其中 tar 是Mac 系統自帶的命令。 - 從終端進入到解壓資料夾r
zip壓縮檔案處理方案(Zip4j壓縮和解壓)
主要特性 Create, Add, Extract, Update, Remove files from a Zip file針對ZIP壓縮檔案建立、新增、抽出、更新和移除檔案 Read/Write password protected Zip files(讀寫有密碼保護的Zip檔案
PAT乙級——1078(字串壓縮和解壓 判斷邊界)Java實現
題目:字串壓縮與解壓 (20 分) 文字壓縮有很多種方法,這裡我們只考慮最簡單的一種:把由相同字元組成的一個連續的片段用這個字元和片段中含有這個字元的個數來表示。例如 ccccc 就用 5c 來表示。如果字元沒有重複,就原樣輸出。例如 aba 壓縮後仍然是 aba。 解壓方法就是
java實現zip的壓縮和解壓
package cn.tzz.zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; i
asp.net 實現檔案的壓縮和解壓
如果該部落格能給您帶來幫助,請給博主一個評論謝謝!!話不多說下面請看具體的實現步驟。 1.首先在web專案中引用ICSharpCode.SharpZipLib.dll檔案,可在博主的資源中下載。 2.具體的壓縮和解壓方法實現如下(程式碼中有詳細的備註) /// <summar
Android 解壓zip檔案(支援中文)
PS:用於解壓縮 Google Play 上面下載下來的 obb 擴充套件檔案! 過了n多天後,當再次使用原先部落格上寫的那篇: 去做zip包的解壓的時候,出現了原來沒有發現的很多問題。首先是中文漢字問題,使用java的zip包不能很好的解決解壓問題;其次還有
哈夫曼編碼實現檔案的壓縮和解壓
哈夫曼編碼的概念 哈夫曼編碼是基於哈夫曼樹實現的一種檔案壓縮方式。 哈夫曼樹:一種帶權路徑最短的最優二叉樹,每個葉子結點都有它的權值,離根節點越近,權值越小(根節點權值為0,往下隨深度增加依次加一),樹的帶權路徑等於各個葉子結點的數值與其權值的乘積和。哈夫曼樹如圖: 從圖中我們可以看出