目录结构如下:

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的更多相关文章

  1. Linux下的压缩zip,解压缩unzip命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

  2. 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 ...

  3. JAVA zip解压 MALFORMED 错误

    最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...

  4. Linux下的压缩zip,解压缩unzip命令具体解释及实例

    实例:压缩server上当前文件夹的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前文件夹 unzip filename.zip ================= ...

  5. java Zip文件解压缩

    java Zip文件解压缩 为了解压缩zip都折腾两天了,查看了许多谷歌.百度来的code, 真实无语了,绝大多数是不能用的.这可能跟我的开发环境有关吧. 我用的是Ubuntu14.04,eclips ...

  6. java zip 压缩与解压

    java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...

  7. linux下压缩与解压(zip、unzip、tar)详解

    linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...

  8. java ZIP压缩文件

    问题描述:     使用java ZIP压缩文件和目录 问题解决:     (1)单个文件压缩 注:     以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...

  9. Linux命令zip和unzip

    问题描述:        使用Linux中命令zip和unzip 问题解决: 命令名: zip  功能说明:压缩文件. 语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][- ...

随机推荐

  1. [CLR via C#]9. 参数

    一.可选参数和命名参数 在设计一个方法的参数时,可为部分或全部参数分配默认值.然后,调用这些方法的代码时可以选择不指定部分实参,接受默认值.此外,调用方法时,还可以通过指定参数名称的方式为其传递实参. ...

  2. Python 3.X 实现定时器 Timer,制作抽象的Timer定时器基类

    Python 在不依赖第三方库的前提下,对于定时器的实现并不是很完美,但是这不意味着我们无法实现. 阅读了网上的一些资料,得出一些结论,顺手写了一个基类的定时器(Python3) BaseTimer: ...

  3. Hibernate框架之Criteria查询

    首先给大家说说Hibernate检索方式 Hibernate提供了5种检索对象的方式 1.导航对象图检索方式:根据已经加载的对象导航到其他对象 2.OID检索方式:按照对象的OID来检索对象 3.HQ ...

  4. XML的约束(schema)

    XML Schema也是一种用于定义和描述XML文档结构与内容的模式语言,其出现是为了克服DTD的局限性 XML Schema符合XML语法结构 DOM.SAX等XML API很容易解析出XML Sc ...

  5. 关于字符串replace方法第二个参数探究

    网上有关replace的文章很多了,这里主要聊聊它的第二个参数.阅读本文需要对replace方法有一定了解.W3school=>replace 我们要把一段字符串中的某些指定字符替换掉,第一时间 ...

  6. css实现垂直居中的方法

    1,设置其line-height值,使之与其高度相同 2,设置table结构,用vertical-align:middle; 3,应用定位,父级别:position:relative:子级:posit ...

  7. lazyload.js详解

    简介 lazyload.js用于长页面图片的延迟加载,视口外的图片会在窗口滚动到它的位置时再进行加载,这是与预加载相反的. 优点: 它可以提高页面加载速度: 在某些情况清晰它也可以帮助减少服务器负载. ...

  8. Access sql语句创建表及字段类型

    创建一张空表: Sql="Create TABLE [表名]" 创建一张有字段的表: Sql="Create TABLE [表名]([字段名1] MEMO NOT NUL ...

  9. UC如被百度控股,手机qq浏览器改如何进攻和防守

    很早以前在公司内部论坛里写的一篇文章,绯闻已经过过去了,现在已物事人物,UC已有阿里大靠山了. ----------------------------------------------- 据网络媒 ...

  10. iOS多线程-02-GCD

    简介 GCD(Grand Center Dispatch)是Apple为多核的并行运算提出的解决方案,纯C语言 更加适配多核处理器,且自动管理线程的生命周期,使用起来较为方便 GCD通过任务和队列实现 ...