Java使用GZIP进行压缩和解压缩

  1. import java.util.zip.GZIPOutputStream;   
  2. import java.io.FileOutputStream;   
  3. import java.io.FileInputStream;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.IOException;   
  6.   
  7. public class CompressFileGZIP {   
  8.   
  9.     /**  
  10.      * Perform file compression.  
  11.      * @param inFileName Name of the file to be compressed  
  12.      */  
  13.     private static void doCompressFile(String inFileName) {   
  14.   
  15.         try {   
  16.           
  17.             System.out.println("Creating the GZIP output stream.");   
  18.             String outFileName = inFileName + ".gz";   
  19.             GZIPOutputStream out = null;   
  20.             try {   
  21.                 out = new GZIPOutputStream(new FileOutputStream(outFileName));   
  22.             } catch(FileNotFoundException e) {   
  23.                 System.err.println("Could not create file: " + outFileName);   
  24.                 System.exit(1);   
  25.             }   
  26.                       
  27.   
  28.             System.out.println("Opening the input file.");   
  29.             FileInputStream in = null;   
  30.             try {   
  31.                 in = new FileInputStream(inFileName);   
  32.             } catch (FileNotFoundException e) {   
  33.                 System.err.println("File not found. " + inFileName);   
  34.                 System.exit(1);   
  35.             }   
  36.   
  37.             System.out.println("Transfering bytes from input file to GZIP Format.");   
  38.             byte[] buf = new byte[1024];   
  39.             int len;   
  40.             while((len = in.read(buf)) > 0) {   
  41.                 out.write(buf, 0, len);   
  42.             }   
  43.             in.close();   
  44.   
  45.             System.out.println("Completing the GZIP file");   
  46.             out.finish();   
  47.             out.close();   
  48.           
  49.         } catch (IOException e) {   
  50.             e.printStackTrace();   
  51.             System.exit(1);   
  52.         }   
  53.   
  54.     }   
  55.   
  56.     /**  
  57.      * Sole entry point to the class and application.  
  58.      * @param args Array of String arguments.  
  59.      */  
  60.     public static void main(String[] args) {   
  61.       
  62.         if (args.length != 1) {   
  63.             System.err.println("Usage: java CompressFileGZIP filename");   
  64.         } else {   
  65.             doCompressFile(args[0]);   
  66.         }   
  67.   
  68.               
  69.     }   
  70.   
  71. }   
  72.   
  73. import java.util.zip.GZIPInputStream;   
  74. import java.io.FileOutputStream;   
  75. import java.io.FileInputStream;   
  76. import java.io.FileNotFoundException;   
  77. import java.io.IOException;   
  78.   
  79. public class UncompressFileGZIP {   
  80.   
  81.     /**  
  82.      * Uncompress the incoming file.  
  83.      * @param inFileName Name of the file to be uncompressed  
  84.      */  
  85.     private static void doUncompressFile(String inFileName) {   
  86.   
  87.         try {   
  88.   
  89.             if (!getExtension(inFileName).equalsIgnoreCase("gz")) {   
  90.                 System.err.println("File name must have extension of \".gz\"");   
  91.                 System.exit(1);   
  92.             }   
  93.   
  94.             System.out.println("Opening the compressed file.");   
  95.             GZIPInputStream in = null;   
  96.             try {   
  97.                 in = new GZIPInputStream(new FileInputStream(inFileName));   
  98.             } catch(FileNotFoundException e) {   
  99.                 System.err.println("File not found. " + inFileName);   
  100.                 System.exit(1);   
  101.             }   
  102.   
  103.             System.out.println("Open the output file.");   
  104.             String outFileName = getFileName(inFileName);   
  105.             FileOutputStream out = null;   
  106.             try {   
  107.                 out = new FileOutputStream(outFileName);   
  108.             } catch (FileNotFoundException e) {   
  109.                 System.err.println("Could not write to file. " + outFileName);   
  110.                 System.exit(1);   
  111.             }   
  112.   
  113.             System.out.println("Transfering bytes from compressed file to the output file.");   
  114.             byte[] buf = new byte[1024];   
  115.             int len;   
  116.             while((len = in.read(buf)) > 0) {   
  117.                 out.write(buf, 0, len);   
  118.             }   
  119.   
  120.             System.out.println("Closing the file and stream");   
  121.             in.close();   
  122.             out.close();   
  123.           
  124.         } catch (IOException e) {   
  125.             e.printStackTrace();   
  126.             System.exit(1);   
  127.         }   
  128.   
  129.     }   
  130.   
  131.     /**  
  132.      * Used to extract and return the extension of a given file.  
  133.      * @param f Incoming file to get the extension of  
  134.      * @return <code>String</code> representing the extension of the incoming  
  135.      *         file.  
  136.      */    
  137.     public static String getExtension(String f) {   
  138.         String ext = "";   
  139.         int i = f.lastIndexOf('.');   
  140.   
  141.         if (i > 0 &&  i < f.length() - 1) {   
  142.             ext = f.substring(i+1);   
  143.         }          
  144.         return ext;   
  145.     }   
  146.   
  147.     /**  
  148.      * Used to extract the filename without its extension.  
  149.      * @param f Incoming file to get the filename  
  150.      * @return <code>String</code> representing the filename without its  
  151.      *         extension.  
  152.      */    
  153.     public static String getFileName(String f) {   
  154.         String fname = "";   
  155.         int i = f.lastIndexOf('.');   
  156.   
  157.         if (i > 0 &&  i < f.length() - 1) {   
  158.             fname = f.substring(0,i);   
  159.         }          
  160.         return fname;   
  161.     }   
  162.   
  163.     /**  
  164.      * Sole entry point to the class and application.  
  165.      * @param args Array of String arguments.  
  166.      */  
  167.     public static void main(String[] args) {   
  168.       
  169.         if (args.length != 1) {   
  170.             System.err.println("Usage: java UncompressFileGZIP gzipfile");   
  171.         } else {   
  172.             doUncompressFile(args[0]);   
  173.         }   
  174.   
  175.     }   
  176.   
  177. }   
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓