導航:首頁 > 文件處理 > javagz文件壓縮

javagz文件壓縮

發布時間:2023-01-02 10:05:11

① 如何解壓.tar.gz gzip gz 類型文檔

java壓縮.gz .zip .tar.gz等格式的壓縮包方法總結
1、.gz文件是linux下常見的壓縮格式。使用 java.util.zip.GZIPInputStream即可,壓縮是 java.util.zip.GZIPOutputStream

1 public static void unGzipFile(String sourcedir) {
2 String ouputfile = "";
3 try {
4 //建立gzip壓縮文件輸入流
5 FileInputStream fin = new FileInputStream(sourcedir);
6 //建立gzip解壓工作流
7 GZIPInputStream gzin = new GZIPInputStream(fin);
8 //建立解壓文件輸出流
9 ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.'));
10 ouputfile = ouputfile.substring(0,ouputfile.lastIndexOf('.'));
11 FileOutputStream fout = new FileOutputStream(ouputfile);
12
13 int num;
14 byte[] buf=new byte[1024];
15
16 while ((num = gzin.read(buf,0,buf.length)) != -1)
17 {
18 fout.write(buf,0,num);
19 }
20
21 gzin.close();
22 fout.close();
23 fin.close();
24 } catch (Exception ex){
25 System.err.println(ex.toString());
26 }
27 return;
28 }

2、zip文件,使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile

1 /**
2 * 解壓縮zipFile
3 * @param file 要解壓的zip文件對象
4 * @param outputDir 要解壓到某個指定的目錄下
5 * @throws IOException
6 */
7 public static void unZip(File file,String outputDir) throws IOException {
8 ZipFile zipFile = null;
9
10 try {
11 Charset CP866 = Charset.forName("CP866"); //specifying alternative (non UTF-8) charset
12 //ZipFile zipFile = new ZipFile(zipArchive, CP866);
13 zipFile = new ZipFile(file, CP866);
14 createDirectory(outputDir,null);//創建輸出目錄
15
16 Enumeration<?> enums = zipFile.entries();
17 while(enums.hasMoreElements()){
18
19 ZipEntry entry = (ZipEntry) enums.nextElement();
20 System.out.println("解壓." + entry.getName());
21
22 if(entry.isDirectory()){//是目錄
23 createDirectory(outputDir,entry.getName());//創建空目錄
24 }else{//是文件
25 File tmpFile = new File(outputDir + "/" + entry.getName());
26 createDirectory(tmpFile.getParent() + "/",null);//創建輸出目錄
27
28 InputStream in = null;
29 OutputStream out = null;
30 try{
31 in = zipFile.getInputStream(entry);;
32 out = new FileOutputStream(tmpFile);
33 int length = 0;
34
35 byte[] b = new byte[2048];
36 while((length = in.read(b)) != -1){
37 out.write(b, 0, length);
38 }
39
40 }catch(IOException ex){
41 throw ex;
42 }finally{
43 if(in!=null)
44 in.close();
45 if(out!=null)
46 out.close();
47 }
48 }
49 }
50
51 } catch (IOException e) {
52 throw new IOException("解壓縮文件出現異常",e);
53 } finally{
54 try{
55 if(zipFile != null){
56 zipFile.close();
57 }
58 }catch(IOException ex){
59 throw new IOException("關閉zipFile出現異常",ex);
60 }
61 }
62 }
63
64 /**
65 * 構建目錄
66 * @param outputDir
67 * @param subDir
68 */
69 public static void createDirectory(String outputDir,String subDir){
70 File file = new File(outputDir);
71 if(!(subDir == null || subDir.trim().equals(""))){//子目錄不為空
72 file = new File(outputDir + "/" + subDir);
73 }
74 if(!file.exists()){
75 if(!file.getParentFile().exists())
76 file.getParentFile().mkdirs();
77 file.mkdirs();
78 }
79 }

3、.tar.gz文件可以看做先用tar打包,再使用gz進行壓縮。
使用org.apache.tools.tar.TarEntry; org.apache.tools.tar.TarInputStream 和 org.apache.tools.tar.TarOutputStream

1 //------------------------------------------------------------------------------------------------------
2 /**
3 * 解壓tar.gz 文件
4 * @param file 要解壓的tar.gz文件對象
5 * @param outputDir 要解壓到某個指定的目錄下
6 * @throws IOException
7 */
8 public static void unTarGz(File file,String outputDir) throws IOException{
9 TarInputStream tarIn = null;
10 try{
11 tarIn = new TarInputStream(new GZIPInputStream(
12 new BufferedInputStream(new FileInputStream(file))),
13 1024 * 2);
14
15 createDirectory(outputDir,null);//創建輸出目錄
16
17 TarEntry entry = null;
18 while( (entry = tarIn.getNextEntry()) != null ){
19
20 if(entry.isDirectory()){//是目錄
21 entry.getName();
22 createDirectory(outputDir,entry.getName());//創建空目錄
23 }else{//是文件
24 File tmpFile = new File(outputDir + "/" + entry.getName());
25 createDirectory(tmpFile.getParent() + "/",null);//創建輸出目錄
26 OutputStream out = null;
27 try{
28 out = new FileOutputStream(tmpFile);
29 int length = 0;
30
31 byte[] b = new byte[2048];
32
33 while((length = tarIn.read(b)) != -1){
34 out.write(b, 0, length);
35 }
36
37 }catch(IOException ex){
38 throw ex;
39 }finally{
40
41 if(out!=null)
42 out.close();
43 }
44 }
45 }
46 }catch(IOException ex){
47 throw new IOException("解壓歸檔文件出現異常",ex);
48 } finally{
49 try{
50 if(tarIn != null){
51 tarIn.close();
52 }
53 }catch(IOException ex){
54 throw new IOException("關閉tarFile出現異常",ex);
55 }
56 }
57 }

使用到的包頭有:

1 import java.io.BufferedInputStream;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import java.nio.charset.Charset;
10 import java.util.Enumeration;
11 import java.util.zip.GZIPInputStream;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipFile;
14
15 import org.apache.tools.tar.TarEntry;
16 import org.apache.tools.tar.TarInputStream;
17 import org.apache.tools.tar.TarOutputStream;

② gz後綴怎麼解壓

1、在Windows系統環境下,安裝解壓軟體

③ 壓縮文件的類型有那些

歸結一下,據我所知一般有以下幾種:

.rar (現在比較流行的壓縮格式,我們幾乎都用它)
.zip (老牌的壓縮格式)
.cab (windows安裝程序的專用壓縮格式,經常可以在安裝包下看到)
.iso (winiso的光碟鏡像格式,通常用於虛擬光碟機,可以用rar查看)
.jar (Java程序的壓縮包格式)
.ace (winace的壓縮格式)
.7z (7-Zip壓縮格式,號稱有著現今最高壓縮比的,我沒有用過- -)
.tar (也是java程序的壓縮格式,通常linux生成的就是)
.gz (http上可以改善web程序性能,linux下用於文件壓縮,通常可以看見.tar.gz的後綴)
.arj (DOS下最好的壓縮工具)
.lzh (比較古老的壓縮格式)
.uue (unix中採用uuencode編碼格式生成的文件)
.bz2 (linux下的壓縮格式,同.gz,但壓縮比要高於.gz)
.z (linux下的壓縮格式,同.gz)

以上格式都可用WinRAR解開

④ 用java如何解析gz文件

一個偷懶的做法是調用操作系統命令把gz解壓縮,然後再讀取。網上也許能找到一些操作gz的java庫。

⑤ java里怎麼解壓tar.gz文件啊,網上好多例子都不行

最後怎麼解決的,我現在也遇到這個問題了,單個文件可以解壓可以壓縮,寫入的測試內容也在,換成文件夾就不行了。能找到的案例全都是解壓成文件,但是本身是個文件夾的GZ包解壓了以後也打不開。

閱讀全文

與javagz文件壓縮相關的資料

熱點內容
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:92
php中sleep 瀏覽:488
vr怎麼看視頻演算法 瀏覽:84
手機app如何申報個人所得稅零申報 瀏覽:692
如何截獲手機app連接的ip 瀏覽:330
冰箱壓縮機是否需要電容 瀏覽:344
python列表每一行數據求和 瀏覽:274
自己有一台伺服器可以玩什麼 瀏覽:656
社會學波普諾pdf 瀏覽:584
解壓做食物的小視頻 瀏覽:758
pdf怎麼單獨設置文件夾 瀏覽:474
業務邏輯程序員 瀏覽:659
addto新建文件夾什麼意思 瀏覽:161
有伺服器地址怎麼安裝軟體 瀏覽:660
安卓如何完全清除數據 瀏覽:691
安卓安卓證書怎麼信任 瀏覽:54
伺服器被攻擊如何解決 瀏覽:221
學霸變成程序員 瀏覽:883
c語言編譯錯誤fatalerror 瀏覽:443
ipv4內部伺服器地址怎麼分配 瀏覽:464