java程序解压/压缩.gz文件
压缩成.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文件相关推荐
- java中解压tar.gz文件
在开发中我们经常需要对gz文件进行解压缩,在java中解压gz文件还是比较繁琐的,为此写了一个工具类方便需要的时候可以直接拿过来用.代码如下: package com.eggsl.utils;impo ...
- win系统压缩/解压.tar.gz 文件的方法
一.将文件压缩成.tar.gz 文件 进入到被压缩文件的文件夹,点击右键打开Windows PoweShell,输入下面命令: tar -cvzf file_name.tar.gz "pat ...
- 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 ...
- 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 ...
- linux解压tar.gz报错,Linux解压tar.gz文件时提示gzip:stdin:not错误
我们都知道Linux系统下的tar命令可用于文件的解压缩,在利用tar命令解压tar.gz文件的时候,提示gzip:stdin:not in gzip format错误,遇到这种情况该如何解决呢?随小 ...
- linux 解压.tar.gz文件
(1)解压 .tar.gz文件 tar -zxvf 文件名.tar.gz 其中,文件名.tar.gz 是你要解压的文件的名称. 解释一下命令的选项: -z:表示使用 gzip 压缩算法进行解压. -x ...
- linux解压tar.gz报错,Linux解压tar.gz文件时提示gzip:stdin:not怎么办?
我们都知道Linux系统下的tar命令可用于文件的解压缩,在利用tar命令解压tar.gz文件的时候,提示gzip:stdin:not in gzip format错误,遇到这种情况该如何解决呢?随小 ...
- 解压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_ ...
- linux解压tar.gz文件,linux tar.gz压缩解压命令详解
linux tar.gz命令是一个常见的文件解压缩命令,那么它具体用法是怎样的呢?下面由学习啦小编为大家整理了linux tar.gz命令的相关知识,希望对大家有帮助! 1.linux tar.gz压 ...
最新文章
- 读书--编写高质量代码 改善C#程序的157个建议2
- gin将请求体绑定到不同的结构体中
- Python爬虫的框架有哪些?推荐这五个!
- 根据需要通过代码的方式加载js文件
- AQS抽象队列同步器详解(待更新)
- Dubbo核心源码之SPI扩展
- HTML入门:html常用的标签
- 计算机视觉(CV)前沿国际国内期刊与会议
- 使用R语言中的GWmodel进行GWR模型的运算
- javaee之用户信息增删改查案例
- Hadoop(一)如何搭建大数据Hadoop集群?(大数据入门教程)
- Openssl CA证书生成以及双向认证,及windows系统证书批量导出,android cer转bks
- Mac上使用微信读书(微信读书网页版)
- u盘win7纯净版_U盘PE启动安装Win7系统教程(微PE版)
- InvocationException: GraphViz‘s executables not found【BUG已解决】
- SQLiteManager 序列号 破解
- 国际移动设备识别码IMEI
- 【Kubernetes离线安装】
- Warning: Data truncated for column at row 1
- 网页制作基础学习——HTML+CSS常用代码