当前位置:首页 > 开发 > 编程语言 > Java > 正文

java 文件压缩 GZIP

发表于: 2013-07-03   作者:dreamoftch   来源:转载   浏览次数:
摘要: 转自:http://www.iteye.com/topic/894879   压缩文件: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.

转自:http://www.iteye.com/topic/894879

 

压缩文件:

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); 
        
  
             
    } 
} 

 解压文件:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class UncompressFileGZIP { 

    /** 
     * Uncompress the incoming file. 
     * @param inFileName Name of the file to be uncompressed 
     */ 
    private static void doUncompressFile(String inFileName) { 

        try { 

            if (!getExtension(inFileName).equalsIgnoreCase("gz")) { 
                System.err.println("File name must have extension of \".gz\""); 
                System.exit(1); 
            } 

            System.out.println("Opening the compressed file."); 
            GZIPInputStream in = null; 
            try { 
                in = new GZIPInputStream(new FileInputStream(inFileName)); 
            } catch(FileNotFoundException e) { 
                System.err.println("File not found. " + inFileName); 
                System.exit(1); 
            } 

            System.out.println("Open the output file."); 
            String outFileName = getFileName(inFileName); 
            FileOutputStream out = null; 
           try { 
                out = new FileOutputStream(outFileName); 
            } catch (FileNotFoundException e) { 
                System.err.println("Could not write to file. " + outFileName); 
                System.exit(1); 
            } 

            System.out.println("Transfering bytes from compressed file to the output file."); 
            byte[] buf = new byte[1024]; 
            int len; 
            while((len = in.read(buf)) > 0) { 
                out.write(buf, 0, len); 
            } 

            System.out.println("Closing the file and stream"); 
            in.close(); 
            out.close(); 
        
        } catch (IOException e) { 
            e.printStackTrace(); 
            System.exit(1); 
        } 

    } 

    /** 
     * Used to extract and return the extension of a given file. 
     * @param f Incoming file to get the extension of 
     * @return <code>String</code> representing the extension of the incoming 
     *         file. 
     */ 
    public static String getExtension(String f) { 
        String ext = ""; 
        int i = f.lastIndexOf('.'); 

        if (i > 0 &&  i < f.length() - 1) { 
            ext = f.substring(i+1); 
        }      
        return ext; 
    } 

    /** 
     * Used to extract the filename without its extension. 
     * @param f Incoming file to get the filename 
     * @return <code>String</code> representing the filename without its 
     *         extension. 
     */ 
    public static String getFileName(String f) { 
        String fname = ""; 
        int i = f.lastIndexOf('.'); 

        if (i > 0 &&  i < f.length() - 1) { 
            fname = f.substring(0,i); 
        }      
        return fname; 
    } 

    /** 
     * Sole entry point to the class and application. 
     * @param args Array of String arguments. 
     */ 
    public static void main(String[] args) { 
    
       
            doUncompressFile("E:\\temp\\new_upload\\Upload.gz"); 
       

    } 

}

 

java 文件压缩 GZIP

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹
java压缩 GZIP进行简单压缩,ZIP进行多文件保存 package org.rui.io.compress; import java.io.Buff
安装: 下载该文章附件 gzip-1.3.12-1-setup.rar (注:该版本只适用于windows) 获取其他版本:http
   GZIP压缩:将压缩后的文本文件,发送给浏览器,减少流量。 一、进行gzip压缩条件:   1、请
一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示
提高加载速度。 刚开始的解决方案是(参考网络上别人写的): 自定义一个 CompressionHttpHandler,实
昨天一篇GZIP相关实现引起大家的广泛关注,为一位朋友验证一下浏览器对gzip的支持情况,才有此文!
昨天一篇GZIP相关实现引起大家的广泛关注,为一位朋友验证一下浏览器对gzip的支持情况,才有此文!
<filter> <filter-name>ZGIPFilter</filter-name> <filter-class>com.ztes
昨天一篇GZIP相关实现引起大家的广泛关注,为一位朋友验证一下浏览器对gzip的支持情况,才有此文!
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号