1. java里怎麼解壓tar.gz文件啊,網上好多例子都不行
我覺得你的步驟有問題,tar.gz壓縮包里放文件或文件夾都無所謂,需要用程序來生成,下面詳細說明:
1.
用程序中的方法【archive】生成tar壓縮文件
2.
用程序中的方法【compressArchive】生成tar.gz壓縮文件
3.
將生成的壓縮文件為參數進行解壓,具體是:
unCompressArchiveGz("d:\\test\\xmlbak.tar.gz");//解壓
4.
查看解壓後的文件夾內容和文件內容,均可以正常顯示訪問
樓主的問題主要是手動生成了一個壓縮文件,這是主要的問題原因。
2. gz後綴怎麼解壓
1、在系統環境下,安裝解壓軟體

3. java如何解壓.gz後綴的壓縮包
File file = new File(zipFilePath); 將zip文件路徑轉換 成文件
zipFile = new ZipFile(file); 調用java util下面的zipfile類
Enumeration<?> zipEnum = zipFile.entries(); 將zip文件裡面的內容都放在迭代器裡面了
ZipEntry entry = (ZipEntry) zipEnum.nextElement();,然後迭代出ZipEntry對象。
zipFile.getInputStream(entry)就可以得到所需要的流了,之後做你需要的操作。
4. 如何通過java,不進行解壓就把iso、apk、gz等壓縮文件中的文件名讀取出來求可行的思路!謝謝!
對著壓縮包,右JI,通過WINRAR打開,通過左鍵直接把文件拖出來,再重命名,再拖進去,就行了
5. java spark獲取gz文件時怎樣分區
spark 讀取HDFS文件,首先會判斷文件是否可分,可切分即可分成多個partition,不可切分則整個文件為一個partition ,gz等壓縮文件屬於不可切分文件,所以設置分區無效,如果想要可變分區,需要對文件加索引支持可切分
6. ".tar"是什麼格式,要如何打開
要打開TAR,使用Java API
Java API提供了解壓縮GZIP和ZIP文件的功能。然而還有許多其他的壓縮格式它並不支持。其中一種最普通格式就是TAR文檔格式。這里有一個可以通過Java來讀.tar文件的API。
Ice Tar API和java.util.zip API的使用方法很相似。一個.tar文件通過TarInputStream被讀取為一系列TarEntry對象。一個TarEntry對象具有多種運算和特徵信息,包括名稱,是否是個目錄,以及它所包含的數據。
以下是一個簡單的代碼片斷:
String filename = "somefile.tar";
 String directory = "somedirectory/";
 OutputStream out = null;
 try {
 // open up the .tar file
 TarInputStream in = new TarInputStream
( new FileInputStream(
new File(filename) )
 );
 TarEntry entry = null;
 // loop over each file/directory in the .tar
 while( (entry = in.getNextEntry()) != null) {
 // ignore directories
 if(entry.isDirectory()) {
 continue;
 }
 // create a file to output to
 File outfile = new File
(directory+entry.getName());
 // make any missing directories
 new File(outfile.getParent()).mkdirs();
  // create an output stream to write to
 out = new BufferedOutputStream
( new FileOutputStream( outfile ) );
 // write out the tar-entry
 int x = 0;
 while( (x = in.read()) != -1) {
 out.write(x);
 }
 out.close();
 }
 in.close();
 } catch(IOException ioe) {
 ioe.printStackTrace();
 // close off streams etc..
 }
下面介紹一個有用的指令:File.mkdirs()。我們來假設一個路徑:
/home/javauser/com/generationjava/files/Example.java
事實上,現在只有/home/javauser/是已經存在的,那麼mkdirs()將生成com/, generationjava/, 和 files/這些目錄。
Ice Tar API使得應用Java的人不僅僅可以處理ZIP文檔,而且還可以處理其他格式的文檔。通過把java.util.zip.GzipInputStream應用到Ice Tar API中,tar.gz文件就可以很容易的讀取了。
7. java里怎麼解壓tar.gz文件啊,網上好多例子都不行
我覺得你的步驟有問題,tar.gz壓縮包里放文件或文件夾都無所謂,需要用程序專來生成,屬下面詳細說明:
用程序中的方法【archive】生成tar壓縮文件
用程序中的方法【compressArchive】生成tar.gz壓縮文件
將生成的壓縮文件為參數進行解壓,具體是:
unCompressArchiveGz("d:\test\xmlbak.tar.gz");//解壓
查看解壓後的文件夾內容和文件內容,均可以正常顯示訪問
樓主的問題主要是手動生成了一個壓縮文件,這是主要的問題原因。
8. 用java如何解析gz文件
一個偷懶的做法是調用操作系統命令把gz解壓縮,然後再讀取。網上也許能找到一些操作gz的java庫。
9. 如何解壓.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;
10. 如何讓Hadoop讀取以gz結尾的文本格式的文件
分析過程:
通過上面的異常,立馬猜想到是由於我的文件是gz結尾,所以hadoop把它當作了壓縮文件,然後嘗試解壓縮後讀取,所以解壓失敗了。於是去問google,沒有搜到能夠直接解決我問題的答案,但是搜到了此處相關的源代碼:LineRecordReader.java;於是嘗試著去閱讀代碼來解決問題,這個類很簡單,繼承自RecordReader,沒有看到next函數和readLine函數,那就應該是基類實現的。很快發現了看名字是跟壓縮解碼相關的代碼:
private CompressionCodecFactory compressionCodecs = null;
...
compressionCodecs = new CompressionCodecFactory(job);
final CompressionCodec codec = compressionCodecs.getCodec(file);
...
if (codec != null) {
    in = new LineReader(codec.createInputStream(fileIn), job);
}
else{
    ...
    in = new LineReader(fileIn, job);
}
 
此處file就是拿到的文件路徑,可以看到,應該就是通過CompressionCode.getCode(file)函數,拿到的codec類,然後讀取的時候出異常了。那怎麼讓MapRece程序把這個.gz文件當作普通的文本文件呢?再點進去看CompressionCodeFactory.java的代碼。getCodec函數的代碼如下:
/**
* Find the relevant compression codec for the given file based on its
* filename suffix.
* @param file the filename to check
* @return the codec object
*/
public CompressionCodec getCodec(Path file) {
    CompressionCodec result = null;
    if (codecs != null) {
        String filename = file.getName();
        String reversedFilename = new StringBuffer(filename).reverse().toString();
        SortedMap<String, CompressionCodec> subMap = codecs.headMap(reversedFilename);
        if (!subMap.isEmpty()) {
            String potentialSuffix = subMap.lastKey();
            if (reversedFilename.startsWith(potentialSuffix)) {
                result = codecs.get(potentialSuffix);
            }
       }
    }
    return result;
}