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$][- ...
随机推荐
- GridView 用 checkbox 全选并取值
方法一,用 js 实现 <script type="text/javascript"> //<![CDATA[ function CheckAll(oCheckb ...
- knockout.js的简介和简单使用
1.knockout简介knockout是一个轻量级的UI类库,通过MVVM模式使JavaScript前端UI简单化knockout有四大重要概念:1)声明式绑定:使用简明移读的语法很容易地将模型(m ...
- 自己动手搞定支付宝手机网站支付接口 FOR ECShop
支付宝WAP网站版本的支付接口网上整合的比较少,看到很多网站在卖,顿觉无语. 主要是得自己查看支付宝官方提供的SDK中的开发文档. 支付宝sdk下载地址:https://doc.open.alipay ...
- java jdk environment variables
1. create system variable 2. edit the system path note: ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3. cre ...
- 一些经典===>>用SQL语句操作数据
用SQL语句操作数据 结构化查询语言(Structured Query Language)简称SQL(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言 ...
- jquery重置html form
很多时候在ajax提交或者对话框隐藏之后,我们希望重置默认值以便下次打开对话框时保持干净. 因为jquery选择器返回的是list,并且没有对此提供reset方法,所以需要针对单个元素进行reset. ...
- mysql 5.7.15发布
2016-09-06,mysql发布了5.7更新5.7.15,修复的bug数项目之前的版本已经大大减少,说明越来越稳定了.估计再过三四个版本,就会有很多公司开始考虑生产中使用了.
- SVG基础图形与参数
SVG是什么 SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义WEB上使用的矢量图 SVG 使用 XML 格式定义图形 SVG 图像在缩放时其图形质量不 ...
- Oracle执行计划与统计信息的一些总结
[日期:2011-08-05]来源:Linux社区 作者:wangshengfeng1986211[字体:大 中 小] 2010-07-01 15:03 1.SET AUTOTRACE ON EXP ...
- SharePoint 2013 术语和术语集介绍
托管元数据是一个集中管理的术语的分层集合,我们可以定义术语和术语集,然后将其用作 SharePoint Server 2013 中项目的属性.简单的说,术语是一个可与 SharePoint Serve ...