目录结构如下:

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. 【循序渐进学Python】2. Python中的序列——列表和元组

    序列概览 在Python中有六种内建的序列:列表.元组.字符串.Unicode字符串.buffer对象和xrange对象.在这里暂时只讨论列表和元组.列表和元组的主要区别在于:列表可以修改,元组(不可 ...

  2. 【AngularJS学习笔记】02 小杂烩及学习总结

    表格示例 <div ng-app="myApp" ng-controller="customersCtrl"> <table> < ...

  3. csharp: MVC Controls

    http://mvccontrolstoolkit.codeplex.com/ MVC Controls Toolkit http://mvcjquerycontrols.codeplex.com/  ...

  4. LINUX重启MYSQL的命令

    LINUX重启MYSQL的命令 标签: mysqllinuxservice脚本web服务server 2010-06-25 10:21 62152人阅读 评论(0) 收藏 举报  分类: Linux( ...

  5. bootstrap glyphicon图标无法显示

    如果不注意bootstrap引入css和fonts的规范,则可能会导致bootstrap 在显示glyphicon图标时无法正常显示,显示为方框. 此时可搜索bootstrap.css中的.glyph ...

  6. Xml序列化、反序列化帮助类

    之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用 /// <summary> / ...

  7. c# Json Dictionary序列化和反序列化

    说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承自Dictionary, IXmlSerializable类的自定义类来实现该功能.感觉完全可以把这样的类封装到C#库中,很具 ...

  8. 穷举法破解 zebrone1.1

    系统 : Windows xp 程序 : zebrone1.1 程序下载地址 :http://pan.baidu.com/s/1boqVcU7 要求 : 编写注册机 使用工具 :OD 可在看雪论坛中查 ...

  9. Python: PDB命令

    1. where(w) 找出当前代码运行位置 2. list(l) 显示当前代码的部分上下文 3. list <line number> 显示指定行的上下文 4. list <lin ...

  10. Objective-C 理解之方括号[ ]的使用

      1,用于通知某个对象该做什么,即通知对象去执行某动作. 在Objective-c中,“[]”的意义:它们用于通知某个对象该做什么.这种通知对象执行某中操作的行为被称为“发送消息”. 例如: [sh ...