java解压gz文件

http://panshaobinsb.iteye.com/blog/1566231

下面是网上的代码
http://www.iteye.com/topic/894879

Java代码 复制代码 收藏代码
  1. import java.io.FileInputStream;    
  2. import java.io.FileNotFoundException;    
  3. import java.io.FileOutputStream;    
  4. import java.io.IOException;    
  5. import java.util.zip.GZIPOutputStream;    
  6.   
  7.   
  8. public class CompressFileGZIP {    
  9. private static void doCompressFile(String inFileName) {    
  10.      
  11.         try {    
  12.            
  13.             System.out.println("Creating the GZIP output stream.");    
  14.             String outFileName = inFileName + ".gz";    
  15.             GZIPOutputStream out = null;    
  16.             try {    
  17.                 out = new GZIPOutputStream(new FileOutputStream(outFileName));    
  18.             } catch(FileNotFoundException e) {    
  19.                 System.err.println("Could not create file: " + outFileName);    
  20.                 System.exit(1);    
  21.             }    
  22.                        
  23.      
  24.             System.out.println("Opening the input file.");    
  25.             FileInputStream in = null;    
  26.             try {    
  27.                 in = new FileInputStream(inFileName);    
  28.             } catch (FileNotFoundException e) {    
  29.             System.err.println("File not found. " + inFileName);    
  30.                 System.exit(1);    
  31.             }    
  32.   
  33.             System.out.println("Transfering bytes from input file to GZIP Format.");    
  34.             byte[] buf = new byte[1024];    
  35.             int len;    
  36.             while((len = in.read(buf)) > 0) {    
  37.                 out.write(buf, 0, len);    
  38.             }    
  39.             in.close();    
  40.   
  41.             System.out.println("Completing the GZIP file");    
  42.             out.finish();    
  43.             out.close();    
  44.            
  45.         } catch (IOException e) {    
  46.             e.printStackTrace();    
  47.             System.exit(1);    
  48.         }    
  49.   
  50.     }    
  51.   
  52.     /**   
  53.      * Sole entry point to the class and application.   
  54.      * @param args Array of String arguments.   
  55.      */    
  56.     public static void main(String[] args) {    
  57.     String str="E:\\AUTORUN.INF";    
  58.            
  59.             doCompressFile(str);    
  60.            
  61.      
  62.                 
  63.     }    
  64. }   
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.GZIPOutputStream; public class CompressFileGZIP { 
private static void doCompressFile(String inFileName) { try { System.out.println("Creating the GZIP output stream."); String outFileName = inFileName + ".gz"; GZIPOutputStream out = null; try { out = new GZIPOutputStream(new FileOutputStream(outFileName)); } catch(FileNotFoundException e) { System.err.println("Could not create file: " + outFileName); System.exit(1); } System.out.println("Opening the input file."); FileInputStream in = null; try { in = new FileInputStream(inFileName); } catch (FileNotFoundException e) { System.err.println("File not found. " + inFileName); System.exit(1); } System.out.println("Transfering bytes from input file to GZIP Format."); byte[] buf = new byte[1024]; int len; while((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); System.out.println("Completing the GZIP file"); out.finish(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } /** * Sole entry point to the class and application. * @param args Array of String arguments. */ public static void main(String[] args) { String str="E:\\AUTORUN.INF"; doCompressFile(str); } 
} 











---------------------------------------------------------------------------------------------------------------------------




Java代码 复制代码 收藏代码
  1. import java.util.zip.GZIPInputStream;    
  2. import java.io.FileOutputStream;    
  3. import java.io.FileInputStream;    
  4. import java.io.FileNotFoundException;    
  5. import java.io.IOException;    
  6.   
  7. public class UncompressFileGZIP {    
  8.   
  9.     /**   
  10.      * Uncompress the incoming file.   
  11.      * @param inFileName Name of the file to be uncompressed   
  12.      */    
  13.     private static void doUncompressFile(String inFileName) {    
  14.   
  15.         try {    
  16.   
  17.             if (!getExtension(inFileName).equalsIgnoreCase("gz")) {    
  18.                 System.err.println("File name must have extension of \".gz\"");    
  19.                 System.exit(1);    
  20.             }    
  21.   
  22.             System.out.println("Opening the compressed file.");    
  23.             GZIPInputStream in = null;    
  24.             try {    
  25.                 in = new GZIPInputStream(new FileInputStream(inFileName));    
  26.             } catch(FileNotFoundException e) {    
  27.                 System.err.println("File not found. " + inFileName);    
  28.                 System.exit(1);    
  29.             }    
  30.   
  31.             System.out.println("Open the output file.");    
  32.             String outFileName = getFileName(inFileName);    
  33.             FileOutputStream out = null;    
  34.            try {    
  35.                 out = new FileOutputStream(outFileName);    
  36.             } catch (FileNotFoundException e) {    
  37.                 System.err.println("Could not write to file. " + outFileName);    
  38.                 System.exit(1);    
  39.             }    
  40.   
  41.             System.out.println("Transfering bytes from compressed file to the output file.");    
  42.             byte[] buf = new byte[1024];    
  43.             int len;    
  44.             while((len = in.read(buf)) > 0) {    
  45.                 out.write(buf, 0, len);    
  46.             }    
  47.   
  48.             System.out.println("Closing the file and stream");    
  49.             in.close();    
  50.             out.close();    
  51.            
  52.         } catch (IOException e) {    
  53.             e.printStackTrace();    
  54.             System.exit(1);    
  55.         }    
  56.   
  57.     }    
  58.   
  59.     /**   
  60.      * Used to extract and return the extension of a given file.   
  61.      * @param f Incoming file to get the extension of   
  62.      * @return String representing the extension of the incoming   
  63.      *         file.   
  64.      */    
  65.     public static String getExtension(String f) {    
  66.         String ext = "";    
  67.         int i = f.lastIndexOf('.');    
  68.   
  69.         if (i > 0 &&  i < f.length() - 1) {    
  70.             ext = f.substring(i+1);    
  71.         }         
  72.         return ext;    
  73.     }    
  74.   
  75.     /**   
  76.      * Used to extract the filename without its extension.   
  77.      * @param f Incoming file to get the filename   
  78.      * @return String representing the filename without its   
  79.      *         extension.   
  80.      */    
  81.     public static String getFileName(String f) {    
  82.         String fname = "";    
  83.         int i = f.lastIndexOf('.');    
  84.   
  85.         if (i > 0 &&  i < f.length() - 1) {    
  86.             fname = f.substring(0,i);    
  87.         }         
  88.         return fname;    
  89.     }    
  90.   
  91.     /**   
  92.      * Sole entry point to the class and application.   
  93.      * @param args Array of String arguments.   
  94.      */    
  95.     public static void main(String[] args) {    
  96.        
  97.           
  98.             doUncompressFile("E:\\AUTORUN.INF.gz");    
  99.           
  100.   
  101.     }    
  102.   
  103. }   

 

 

http://blog.csdn.net/hwq1987/article/details/6279130

在解压gz文件时,如果直接用java.util.zip.GZIPInputStream来处理问题只能解压很少一部分内容,通过类MultiMemberGZIPInputStream 可以完全解压一个gz文件。

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部