Java zip and unzip demo
目录结构如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; public class zipDemo { public static void main(String[] args) {
try {
//zipFolder("/home/hadoop/test";);
unzip("/home/hadoop/mytest/test.zip","/home/hadoop/mytest/");
} catch (IOException e) {
e.printStackTrace();
}
} static void zipFolder(String _path) throws IOException
{
Path path = Paths.get(_path);
String target = "/home/hadoop/mytest/test.zip";
//String target = path.getParent() +"/" + path.getFileName() +".zip";
/*
System.out.println(path.getFileName());
System.out.println(path.getRoot());
System.out.println(path.getParent());System.out.println(target);
*/
ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(target));
zipFile(zo,path,"");
zo.close();
}
static void zipFile(ZipOutputStream zo,Path _path,String parentpath) throws IOException
{
File _file = _path.toFile();
if(_file.isFile())
{
byte[] buff = new byte[1024];
FileInputStream fi = new FileInputStream(_file);
int len;
zo.putNextEntry(new ZipEntry(parentpath +"/" + _file.getName()));
while((len=fi.read(buff))>0)
zo.write(buff, 0, len);
zo.closeEntry();
fi.close();
}
if(_file.isDirectory())
{
if(_file.listFiles().length==0)
{
zo.putNextEntry(new ZipEntry(parentpath.equals("")?_file.getName():parentpath + "/" + _file.getName() + "/"));
}
for(File __file : _file.listFiles())
zipFile(zo,__file.toPath(),parentpath.equals("")?_file.getName():parentpath+ "/" + _file.getName());
}
} static void unzip(String path,String target) throws IOException
{
File targetfolder = new File(target);
ZipInputStream zi = new ZipInputStream(new FileInputStream(path));
ZipEntry ze = null;
FileOutputStream fo = null;
byte[] buff = new byte[1024];
int len;
while((ze = zi.getNextEntry())!=null)
{
File _file = new File(targetfolder,ze.getName());
if(!_file.getParentFile().exists()) _file.getParentFile().mkdirs();
if(ze.isDirectory())
{
_file.mkdir();
}
else //file
{
fo = new FileOutputStream(_file);
while((len=zi.read(buff))>0) fo.write(buff, 0, len);
fo.close();
}
zi.closeEntry();
}
zi.close();
}
}
Java zip and unzip demo的更多相关文章
- Linux下的压缩zip,解压缩unzip命令详解及实例
实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...
- Java ZIP File Example---refernce
In this tutorial we are going to see how to ZIP a file in Java. ZIP is an archive file format that e ...
- JAVA zip解压 MALFORMED 错误
最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...
- Linux下的压缩zip,解压缩unzip命令具体解释及实例
实例:压缩server上当前文件夹的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前文件夹 unzip filename.zip ================= ...
- java Zip文件解压缩
java Zip文件解压缩 为了解压缩zip都折腾两天了,查看了许多谷歌.百度来的code, 真实无语了,绝大多数是不能用的.这可能跟我的开发环境有关吧. 我用的是Ubuntu14.04,eclips ...
- java zip 压缩与解压
java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...
- linux下压缩与解压(zip、unzip、tar)详解
linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...
- java ZIP压缩文件
问题描述: 使用java ZIP压缩文件和目录 问题解决: (1)单个文件压缩 注: 以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...
- Linux命令zip和unzip
问题描述: 使用Linux中命令zip和unzip 问题解决: 命令名: zip 功能说明:压缩文件. 语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][- ...
随机推荐
- python代码风格-PEP8
转载自http://www.douban.com/note/134971609/ Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下 ...
- Protocol Buffers动态消息解析
http://www.searchtb.com/2012/09/protocol-buffers.html http://www.cnblogs.com/jacksu-tencent/p/344731 ...
- MyBatis的一些基本操作
在学校里只学过一点点的hibernate基础,但是这几天被熊哥叫去写好几个类的接口,所以就去百度了一下mybatis的接口方式怎么使用.1定义接口,并且定义其中要使用到的方法,这里必须注意到的是方法名 ...
- 《JAVA与模式》之组合模式
定义(GoF<设计模式>): 将对象组合成树形结构以表示“部分整体”的层次结构.组合模式使得用户对单个对象和使用具有一致性. 及角色: 1.Component 是组合中的对象声明接口,在适 ...
- python flask应用部署
失败版本:flask+uwsgi ini配置文件 [uwsgi] callable = app ;//程序内启用的application变量名 home = /home/jcuan/code/pyth ...
- ASP.NET WebAPI 11 参数验证
在绑定完Action的所有参数后,WebAPI并不会马上执行该方法,而要对参数进行验证,以保证输入的合法性. ModelState 在ApiController中一个ModelState属性用来获取参 ...
- 在Hadoop平台跑python脚本
1.开发IDE,我使用的是PyCharm. 2.运行原理 使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入).STDOUT(标准输出)在 ...
- ORACLE 中ROWNUM用法总结!
ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...
- Vue混合
gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson13 一 定位 混合以一种灵活的方式为组件提供分布复用功能.混合对象 ...
- 显示所有SAP图标的ABAP代码
TABLES: icon. INCLUDE <icon>. FIELD-SYMBOLS: <f>. SELECT * FROM icon. ASSIGN (icon-name) ...