admin 管理员组

文章数量: 1184232

Gzip是在* nix系统中压缩文件的流行工具。 但是,Gzip不是ZIP工具, 它仅用于将文件压缩为“ .gz”格式,而不能将多个文件压缩为单个存档

GZip示例

在此示例中,它将压缩文件“ /home/mkyong/file1.txt ”到一个gzip文件–“ /home/mkyong/file1.gz ”。

package com.mkyong.gzip;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
 
public class GZipFile
{
    private static final String OUTPUT_GZIP_FILE = "/home/mkyong/file1.gz";
    private static final String SOURCE_FILE = "/home/mkyong/file1.txt";
 
 
    public static void main( String[] args )
    {
    	GZipFile gZip = new GZipFile();
    	gZip.gzipIt();
    }
 
    /**
     * GZip it
     * @param zipFile output GZip file location
     */
    public void gzipIt(){
 
     byte[] buffer = new byte[1024];
 
     try{
 
    	GZIPOutputStream gzos = 
    		new GZIPOutputStream(new FileOutputStream(OUTPUT_GZIP_FILE));
 
        FileInputStream in = 
            new FileInputStream(SOURCE_FILE);
 
        int len;
        while ((len = in.read(buffer)) > 0) {
        	gzos.write(buffer, 0, len);
        }
 
        in.close();
    	
    	gzos.finish();
    	gzos.close();
 
    	System.out.println("Done");
    	
    }catch(IOException ex){
       ex.printStackTrace();   
    }
   }
 
}

跟进

参考

翻译自:

本文标签: 压缩技术 文件 掌握