压缩成.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

java程序解压/压缩.gz文件相关推荐

  1. java中解压tar.gz文件

    在开发中我们经常需要对gz文件进行解压缩,在java中解压gz文件还是比较繁琐的,为此写了一个工具类方便需要的时候可以直接拿过来用.代码如下: package com.eggsl.utils;impo ...

  2. win系统压缩/解压.tar.gz 文件的方法

    一.将文件压缩成.tar.gz 文件 进入到被压缩文件的文件夹,点击右键打开Windows PoweShell,输入下面命令: tar -cvzf file_name.tar.gz "pat ...

  3. linux解压gz.gz文件,linux解压tar.gz并重命名_linux解压tar.gz文件

    原标题:linux解压tar.gz并重命名_linux解压tar.gz文件 命名为jpg.tar.gz tar –cjf jpg.tar.bz2 *.jpg //将目录里所有jpg文件打包成jpg.t ...

  4. Linux解压tar.gz文件时提示gzip: stdin: not in gzip format tar: Child returned status 1 tar:

    在Linux系统下解压缩tar.gz文件命令文件的时候,提示gzip: stdin: not in gzip format tar: Child returned status 1 tar 在解压ta ...

  5. linux解压tar.gz报错,Linux解压tar.gz文件时提示gzip:stdin:not错误

    我们都知道Linux系统下的tar命令可用于文件的解压缩,在利用tar命令解压tar.gz文件的时候,提示gzip:stdin:not in gzip format错误,遇到这种情况该如何解决呢?随小 ...

  6. linux 解压.tar.gz文件

    (1)解压 .tar.gz文件 tar -zxvf 文件名.tar.gz 其中,文件名.tar.gz 是你要解压的文件的名称. 解释一下命令的选项: -z:表示使用 gzip 压缩算法进行解压. -x ...

  7. linux解压tar.gz报错,Linux解压tar.gz文件时提示gzip:stdin:not怎么办?

    我们都知道Linux系统下的tar命令可用于文件的解压缩,在利用tar命令解压tar.gz文件的时候,提示gzip:stdin:not in gzip format错误,遇到这种情况该如何解决呢?随小 ...

  8. 解压tar.gz文件的时候报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recov

    在解压tar.gz文件的时候报错 [root@master software]# tar -xzvf /opt/software/eclipse-jee-luna-SR2-linux-gtk-x86_ ...

  9. linux解压tar.gz文件,linux tar.gz压缩解压命令详解

    linux tar.gz命令是一个常见的文件解压缩命令,那么它具体用法是怎样的呢?下面由学习啦小编为大家整理了linux tar.gz命令的相关知识,希望对大家有帮助! 1.linux tar.gz压 ...

最新文章

  1. 读书--编写高质量代码 改善C#程序的157个建议2
  2. gin将请求体绑定到不同的结构体中
  3. Python爬虫的框架有哪些?推荐这五个!
  4. 根据需要通过代码的方式加载js文件
  5. AQS抽象队列同步器详解(待更新)
  6. Dubbo核心源码之SPI扩展
  7. HTML入门:html常用的标签
  8. 计算机视觉(CV)前沿国际国内期刊与会议
  9. 使用R语言中的GWmodel进行GWR模型的运算
  10. javaee之用户信息增删改查案例
  11. Hadoop(一)如何搭建大数据Hadoop集群?(大数据入门教程)
  12. Openssl CA证书生成以及双向认证,及windows系统证书批量导出,android cer转bks
  13. Mac上使用微信读书(微信读书网页版)
  14. u盘win7纯净版_U盘PE启动安装Win7系统教程(微PE版)
  15. InvocationException: GraphViz‘s executables not found【BUG已解决】
  16. SQLiteManager 序列号 破解
  17. 国际移动设备识别码IMEI
  18. 【Kubernetes离线安装】
  19. Warning: Data truncated for column at row 1
  20. 网页制作基础学习——HTML+CSS常用代码

热门文章

  1. 解决IDEA中进行maven install报:系统资源不足的问题
  2. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
  3. 菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构
  4. 有关Spring 3.0的发布
  5. CSS3的过渡和动画
  6. GEF常见问题8:导出到图片
  7. 无所不在的物联网设备 你我都需要正视所带来的安全问题
  8. Web Components 简述
  9. Spring读取配置文件,获取bean的几种方式
  10. httpd基于域名不同的虚拟主机配置