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

javagz解壓縮

發布時間:2022-01-26 03:00:32

❶ 如何解壓.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;

❷ java如何解壓.gz後綴的壓縮包

  1. File file = new File(zipFilePath); 將zip文件路徑轉換 成文件

  2. zipFile = new ZipFile(file); 調用java util下面的zipfile類

  3. Enumeration<?> zipEnum = zipFile.entries(); 將zip文件裡面的內容都放在迭代器裡面了

  4. ZipEntry entry = (ZipEntry) zipEnum.nextElement();,然後迭代出ZipEntry對象。

  5. zipFile.getInputStream(entry)就可以得到所需要的流了,之後做你需要的操作。

❸ java端用GZIPOutputStream壓縮的數據,通過HTTP POST到PHP寫的後台,怎麼不能解壓

GZIPOutputStream和PHP的gzuncompress配合得不好,似乎是Java產生的數據頭在PHP那邊認不出來。用DeflaterOutputStream來取代GZIPOutputStream。

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

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

❺ 需求是利用java實現所有常用的壓縮包解壓,分卷解壓功能。我想知道java能做到嗎哪裡有資料,謝謝。

我能建議你起個 process 直接調 7z 的命令行不

❻ 如何通過java,不進行解壓就把iso、apk、gz等壓縮文件中的文件名讀取出來求可行的思路!謝謝!

這個應該是實現不了的

❼ java 解壓不是壓縮的文件

使用java調用winrar解壓,如果需要的話然後讀取解壓後的文件

❽ 用java如何解析gz文件

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

❾ Java 怎麼解壓zlib函數庫壓縮的tar.gz格式的文件

這個文件不是在windows環境下解壓的,它是在linux系統下的壓縮文件。你應該解壓zip格式的壓縮包

❿ 在LINUX下 用JAVA如何解壓rar文件

下載地址:http://www.rarsoft.com/download.htm(目前最新為RAR 3.71 for Linux)

以最新的為准。

對於Window下的常見壓縮文件.zip和.rar,Linux也有相應的方法來解壓它們:

1:對於.zip

linux下提供了zip和unzip程序,zip是壓縮程序,unzip是解壓程序。它們的參數選項很多,這里只做簡單介紹,舉例說明一下其用法:

# zip all.zip *.jpg(這條命令是將所有.jpg的文件壓縮成一個zip包)

# unzip all.zip(這條命令是將all.zip中的所有文件解壓出來)

2:對於.rar

要在linux下處理.rar文件,需要安裝RAR for Linux,可以從網上下載,但要記住,RAR for Linux不是免費的;可從http://www.rarsoft.com/download.htm下載RAR 3。60 for Linux ,然後安裝其安裝操作如下:

# tar -xzpvf rarlinux-3.2.0.tar.gz
# cd rar
# make

這樣就安裝好了,安裝後就有了rar和unrar這兩個程序,rar是壓縮程序,unrar是解壓程序。它們的參數選項很多,舉例說明一下其用法

# rar a all *.jpg

這條命令是將所有.jpg的文件壓縮成一個rar包,名為all.rar,該程序會將.rar 擴展名將自動附加到包名後。

# unrar e all.rar

這條命令是將all.rar中的所有文件解壓出來。

linux下的文件名引用的時候空格要加轉義 比如

test file.rar 實際引用的時候就是 test\ file.rar

至於怎麼解決 我就不再多說了哈

閱讀全文

與javagz解壓縮相關的資料

熱點內容
安卓內錄怎麼錄才能清楚 瀏覽:515
程序員轉正述職報告 瀏覽:799
路由器telnet命令 瀏覽:404
命令符怎麼打開設置 瀏覽:129
考研數學全書pdf 瀏覽:610
linuxgccc 瀏覽:860
單個蘋果耳機怎麼連接安卓 瀏覽:562
塘管家源碼 瀏覽:698
台劇無邪什麼app可以看 瀏覽:586
筆記本配置伺服器怎麼連接 瀏覽:369
android代理上網設置 瀏覽:659
u盤加密不能顯示盤符 瀏覽:665
去伺服器玩什麼游戲 瀏覽:134
哪個同步盤支持多個文件夾 瀏覽:887
蘋果版的我的世界如何轉到安卓 瀏覽:275
linuxswap空間 瀏覽:409
如何搭建網路到各個伺服器 瀏覽:967
oa系統crm源碼 瀏覽:584
安卓生態為什麼一直很爛 瀏覽:147
數字加密傳輸流程 瀏覽:733