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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
2018互联网企业最新面试大纲180+道Java面试题目!含答案解析!
在进入正文之前,顺便给大家推荐一个Java架构方面的交流学习群:878249276,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。相信对于已经工作和遇到技术瓶颈的同学,在这个群里会有你需要的内容。有需要的同学请抓紧时间加入进来。 大厂常见问题 写视频点播网站文件下载接口 基础变量/数组写出模拟maven导入包过程 写出新变脸内存分配,模拟垃圾回收过程 50个白球50个红球,两个盒子,怎么放让人随机在一个盒子里抽到红球概率最高 n个数里取两个和为s的数 java数据结构 HashMap原理 自定义类型可以作为Key么? java内存模型 知道的排序算法 快排的优化 Java多线程实现方式 Java线程与进程区别 JVM内存模型+垃圾回收算法 hashmap和treemap的区别 操作系统同步方式、通信方式 计算机网络三次握手四次分手以及wait_time三种差别 http post和get差别 美赛的建模 k-means 算法 数据库的三范式 路由器和交换机...
-
下一篇
适配器在JavaScript中的体现
适配器设计模式在JavaScript中非常有用,在处理跨浏览器兼容问题、整合多个第三方SDK的调用,都可以看到它的身影。 其实在日常开发中,很多时候会不经意间写出符合某种设计模式的代码,毕竟设计模式就是老前辈们总结提炼出来的一些能够帮助提升开发效率的一些模版,源于日常的开发中。 而适配器其实在JavaScript中应该是比较常见的一种了。 在维基百科中,关于适配器模式的定义为: 在软件工程中,适配器模式是一种软件设计模式,允许从另一个接口使用现有类的接口。它通常用于使现有的类与其他类一起工作,而无需修改其源代码。 生活中的例子 在生活中最常见的就是电源插头的适配器了,世界各国的插座标准各不相同,如果需要根据各国的标准购买对应的电源插头那未免太过于浪费钱财,如果说自己带着插座,把人家墙敲碎,重新接线,也肯定是不现实的。 所以就会有插头的适配器,用来将某种插头转换成另一种插头,在插座和你的电源之间做中转的这个东西,就是适配器。 在代码中的体现 而转向到编程中,我个人是这样理解的: 将那些你不愿意看见的脏代码藏起来,你就可以说这是一个适配器 接入多个第三方SDK 举个日常开发中的例子,我们在...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8编译安装MySQL8.0.19
- Docker容器配置,解决镜像无法拉取问题
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境