IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.html
Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。
我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。
ZipFile
- File file = new File("F:/zippath.zip");
- ZipFile zipFile = new ZipFile(file);
- System.out.println("压缩文件的名称为:" + zipFile.getName());
压缩单个文件
- /** 压缩单个文件*/
- public static void ZipFile(String filepath ,String zippath) {
- try {
- File file = new File(filepath);
- File zipFile = new File(zippath);
- InputStream input = new FileInputStream(file);
- ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
- zipOut.putNextEntry(new ZipEntry(file.getName()));
- int temp = 0;
- while((temp = input.read()) != -1){
- zipOut.write(temp);
- }
- input.close();
- zipOut.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
应用:
- ZipFile("d:/hello.txt", "d:/hello.zip");
压缩多个文件(文件夹)
- /** 一次性压缩多个文件,文件存放至一个文件夹中*/
- public static void ZipMultiFile(String filepath ,String zippath) {
- try {
- File file = new File(filepath);// 要被压缩的文件夹
- File zipFile = new File(zippath);
- InputStream input = null;
- ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
- if(file.isDirectory()){
- File[] files = file.listFiles();
- for(int i = 0; i < files.length; ++i){
- input = new FileInputStream(files[i]);
- zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
- int temp = 0;
- while((temp = input.read()) != -1){
- zipOut.write(temp);
- }
- input.close();
- }
- }
- zipOut.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
应用:成都java培训
- ZipMultiFile("f:/uu", "f:/zippath.zip");
解压缩单个文件
- /** 解压缩(解压缩单个文件)*/ www.cdtarena.com
- public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
- try {
- File file = new File(zippath);//压缩文件路径和文件名
- File outFile = new File(outfilepath);//解压后路径和文件名
- ZipFile zipFile = new ZipFile(file);
- ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
- InputStream input = zipFile.getInputStream(entry);
- OutputStream output = new FileOutputStream(outFile);
- int temp = 0;
- while((temp = input.read()) != -1){
- output.write(temp);
- }
- input.close();
- output.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
应用:
- ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
解压缩多个文件
- /** 解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
- * ZipInputStream类
- * 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
- * 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
- * */
- public static void ZipContraMultiFile(String zippath ,String outzippath){
- try {
- File file = new File(zippath);
- File outFile = null;
- ZipFile zipFile = new ZipFile(file);
- ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
- ZipEntry entry = null;
- InputStream input = null;
- OutputStream output = null;
- while((entry = zipInput.getNextEntry()) != null){
- System.out.println("解压缩" + entry.getName() + "文件");
- outFile = new File(outzippath + File.separator + entry.getName());
- if(!outFile.getParentFile().exists()){
- outFile.getParentFile().mkdir();
- }
- if(!outFile.exists()){
- outFile.createNewFile();
- }
- input = zipFile.getInputStream(entry);
- output = new FileOutputStream(outFile);
- int temp = 0;
- while((temp = input.read()) != -1){
- output.write(temp);
- }
- input.close();
- output.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
应用:
- ZipContraMultiFile("f:/zippath.zip", "d:/");
- ZipContraMultiFile("d:/hello.zip", "d:/");
IO操作之使用zip包压缩和解压缩文件的更多相关文章
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)
1.压缩和解压缩命令 常用压缩格式:.zip..gz..bz2..tar.gz..tar.bz2..rar .zip格式压缩和解压缩命令 zip 压缩文件名 源文件:压缩文件 ...
- Java 的zip压缩和解压缩
Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
[源码下载] 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的 ...
- java采用zip方式实现String的压缩和解压缩CompressStringUtil类
CompressStringUtil类:不多说,直接贴代码: /** * 压缩 * * @param paramString * @return */ public static final byte ...
- Java对zip格式压缩和解压缩
Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...
随机推荐
- 总结一下ASP.NET MVC 网站的部署问题
总结一下ASP.NET MVC 网站的部署问题 近日,准备把MVC建了一个新的测试站点部署到IIS上面,结果没想到出现了一系列的问题和错误,准备记录一下. 第一个问题,就是如何将MVC的站点部署到II ...
- js去除首尾空格
简单的:str = jQuery.trim(str); var temp = " aa b "; console.log("cc" + temp); temp ...
- Centon6.5虚拟机桥接设置
参考资料:http://blog.csdn.net/iamfafa/article/details/6209009 安装虚拟机的时候 就直接选择桥接,可以直接 使用 查看此状态下的配置如下 : 虚拟环 ...
- NSData 数据转换
NSData,数据,当我们需要把一些信息写入到文件里或发送到网络上,我们需要把这些数据转换下,变成纯粹的0.1字符流 数组转 NSData NSData *GLYtime = [NSKeyedArch ...
- HBase 几点思考
1. http://blog.csdn.net/yueyedeai/article/details/14648067 2. http://blog.csdn.net/pirateleo/article ...
- python类继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...
- 淘宝PK京东:哥刷的不是广告,刷的是存在
冯强/文 (昨晚看阿根廷vs瑞士时手机上敲的,看完太激动忘发了,现配了图发上来) 这两天,关于京东.淘宝渠道下沉的新闻中,两家略带喜感的农村墙体广告在互联网上传播,例如以下图: 京东这图片,越看越像P ...
- PHP - 继承 - 子类使用父类方法
<?php class ShopProduct { private $FirstName; private $LastName; private $Title; private $Price; ...
- ASP.net 学习路线(详细)
.net学习路线 入门篇1. 学习面向对象(OOP)的编程思想 许多高级语言都是面向对象的编程,.NET也不例外.如果您第一次接触面向对象的编程,就必须理解类.对象.字段.属性.方法和 ...
- ELK 之二:ElasticSearch 和Logstash高级使用
一:文档 官方文档地址:1.x版本和2.x版本 https://www.elastic.co/guide/en/elasticsearch/guide/index.html 硬件要求: 1.内存,官方 ...