java程序解压/压缩.gz文件

简介: 压缩成.gz格式import java.io.FileInputStream; import java.io.FileNotFoundException; import java.

压缩成.gz格式

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

解压.gz格式

import java.util.zip.GZIPInputStream;   
import java.io.FileOutputStream;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.IOException;   
  
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:\\AUTORUN.INF.gz");   
         
  
    }   
  
}   

  本文出处:http://panshaobinsb.iteye.com/blog/1566231

目录
相关文章
|
2月前
|
监控 Java API
Java语言按文件创建日期排序及获取最新文件的技术
这段代码实现了文件创建时间的读取、文件列表的获取与排序以及获取最新文件的需求。它具备良好的效率和可读性,对于绝大多数处理文件属性相关的需求来说足够健壮。在实际应用中,根据具体情况,可能还需要进一步处理如访问权限不足、文件系统不支持某些属性等边界情况。
131 14
|
2月前
|
存储 Java 编译器
深入理解Java虚拟机--类文件结构
本内容介绍了Java虚拟机与Class文件的关系及其内部结构。Class文件是一种与语言无关的二进制格式,包含JVM指令集、符号表等信息。无论使用何种语言,只要能生成符合规范的Class文件,即可在JVM上运行。文章详细解析了Class文件的组成,包括魔数、版本号、常量池、访问标志、类索引、字段表、方法表和属性表等,并说明其在Java编译与运行过程中的作用。
|
2月前
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
112 0
|
3月前
|
存储 Java 数据安全/隐私保护
Java技术栈揭秘:Base64加密和解密文件的实战案例
以上就是我们今天关于Java实现Base64编码和解码的实战案例介绍。希望能对你有所帮助。还有更多知识等待你去探索和学习,让我们一同努力,继续前行!
268 5
|
3月前
|
网络协议 安全 Java
实现Java语言的文件断点续传功能的技术方案。
像这样,我们就完成了一项看似高科技、实则亲民的小工程。这样的技术实现不仅具备实用性,也能在面对网络不稳定的挑战时,稳稳地、不失乐趣地完成工作。
184 0
|
6月前
|
前端开发 Cloud Native Java
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
|
6月前
|
Java C语言
课时8:Java程序基本概念(标识符与关键字)
课时8介绍Java程序中的标识符与关键字。标识符由字母、数字、下划线和美元符号组成,不能以数字开头且不能使用Java保留字。建议使用有意义的命名,如student_name、age。关键字是特殊标记,如蓝色字体所示。未使用的关键字有goto、const;特殊单词null、true、false不算关键字。JDK1.4后新增assert,JDK1.5后新增enum。
|
6月前
|
Java 编译器
课时7:Java程序基本概念(注释)
课时7介绍了Java程序中的注释。编程语言有其语法和语义,注释有助于理解代码需求,防止断档。Java支持三类注释:单行(//)、多行(/* */)和文档注释(/** */)。注释不会被编译器编译。范例中展示了如何在代码中使用注释,并强调了注释对项目文档管理的重要性。
|
6月前
|
存储 Java 数据库连接
【YashanDB知识库】Java程序调用存储过程,在提取clob时报YAS-00004
【YashanDB知识库】Java程序调用存储过程,在提取clob时报YAS-00004
|
6月前
|
搜索推荐 Java Android开发
课时146:使用JDT开发Java程序
在 Eclipse 之中提供有 JDT环境可以实现java 程序的开发,下面就通过一些功能进行演示。 项目开发流程
228 0

热门文章

最新文章