`
dreamoftch
  • 浏览: 492825 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java 文件压缩 GZIP

阅读更多

转自: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文件压缩与解压缩实践(源代码+论文)

    综上所述,这个实践项目涵盖了Java文件压缩与解压缩的核心技术,是学习和理解Java I/O和压缩库的宝贵资源。通过阅读源代码和论文,开发者不仅可以学习如何在Java中进行文件压缩,还能了解到如何将这些技术应用于实际...

    Java用GZIP压缩解压文件源码

    在Java编程语言中,GZIP是一种常用的文件压缩格式,它基于DEFLATE算法,可以用于压缩和解压缩数据。在本文中,我们将深入探讨如何使用Java实现GZIP压缩和解压缩文件的源码。 首先,我们需要引入Java的`java.util....

    Java用GZIP压缩解压文件.rar

    一个Java随书实例:用GZIP压缩解压文件,从源文件得到文件输入流,得到目标文件输出流,得到压缩输出流,设定读入缓冲区尺寸,弹出文件选择器,并判断是否点击了打开按钮,判断事件来自于哪个按钮,用于选择解压和...

    JAVA源码Java用GZIP压缩解压文件JAVA源码Java用GZIP压缩解压文件

    java,JAVA源码Java用GZIP压缩解压文件JAVA源码Java用GZIP压缩解压文件

    Java用GZIP压缩解压文件

    在Java编程环境中,GZIP是一种常用的文件压缩格式,它基于DEFLATE算法,可以有效地减少文件大小,便于存储和传输。本篇文章将深入探讨如何在Java中使用GZIP进行文件的压缩与解压缩操作。 首先,我们需要了解Java中...

    java自带压缩方式的性能比较

    通过提供的代码文件`CompressTestMain.java`、`GzipUtils.java`和`ZipUtils.java`,我们可以推测作者可能构建了一个测试环境,比较了Gzip和Zip压缩算法在实际操作中的性能差异。 首先,让我们了解Gzip和Zip的基本...

    JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码.zip

    在Java编程语言中,文件的压缩与解压缩是常见的操作,尤其在数据传输、存储优化以及备份场景下显得尤为重要。本实践主要关注如何使用Java来处理ZIP格式的压缩文件,以下将详细介绍相关知识点。 1. **Java档案API...

    JAVA文件压缩与解压缩实践(源代码+论文).rar

    Java文件压缩与解压缩是Java开发中常见的操作,特别是在数据传输、存储优化和备份场景下。本实践项目提供了源代码和相关论文,帮助开发者深入理解这一主题。在Java中,我们可以利用内置的`java.util.zip`包来实现...

    Java用GZIP压缩解压文件.7z

    在Java编程语言中,GZIP是一种常用的文件压缩格式,它基于DEFLATE算法,可以用于压缩和解压缩数据。在处理大文件或者网络传输时,使用GZIP进行压缩可以显著减少数据量,提高效率。本篇文章将深入探讨如何在Java中...

    JAVA文件压缩与解压缩实践的实现.rar

    Java文件压缩与解压缩实践是指利用Java编程语言实现对文件或文件夹进行压缩和解压缩操作。以下是该实践的描述: 技术概述: Java文件压缩与解压缩是指将一个或多个文件或文件夹打包成一个压缩文件,或者将压缩文件...

    JAVA文件压缩与解压缩实践(源代码+论文).zip

    总之,掌握Java文件压缩与解压缩技术对于任何Java开发者来说都是必要的技能,无论是在日常开发还是在特定项目中,这都是一项实用的工具。通过研究提供的源代码和论文,你将能深入理解这一技术,并能够灵活地将其应用...

    java 解压,压缩linux GZ压缩文件

    GZ是一种常用的文件压缩格式,它利用了gzip程序进行压缩,而在Java中,我们可以使用`java.util.zip`包中的类来实现对GZ文件的压缩和解压缩操作。 在给出的代码中,有两个主要的方法:`testGzipOutput()`用于解压GZ...

    基于java的用GZIP压缩解压文件.zip

    总之,这个压缩包文件包含了使用Java进行GZIP压缩和解压缩的基本示例,是学习和理解Java文件压缩技术的好资源。通过学习和实践这些示例,开发者可以掌握如何在实际项目中高效地处理GZIP压缩文件。

    java压缩文件以及文件夹

    首先,让我们了解`java.util.zip`包,它是Java标准库中用于处理ZIP和GZIP格式的压缩的工具。`ZipOutputStream`和`ZipEntry`是这个包中的关键类,它们用于创建和管理ZIP文件。 1. **ZipOutputStream**: - `...

    基于Java的用GZIP压缩解压文件.zip

    在Java编程环境中,GZIP是一种常用的文件压缩格式,它基于DEFLATE算法,可以用于压缩和解压缩数据。本主题将深入探讨如何使用Java来处理GZIP压缩和解压缩文件。 首先,我们要了解Java中的`java.util.zip....

Global site tag (gtag.js) - Google Analytics