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$][- ...
随机推荐
- Oracle备份表结构和数据
--创建一份表结构 create table BASE_GOODSPAYMENT_SETTING_BAK as select * from BASE_GOODSPAYMENT_SETTING ; -- ...
- 页面UI注意事项,你在乎吗?
早上打开微信,看到一篇文章,下面就和大家分享一下,该文章属于前端文章系列,希望做后台开发系统的程序员也可以学习一下,只会写代码把功能实现是第一,接下来也要把界面做做好. 现在的界面风格对于手机而言,一 ...
- 看了一本Unity3D的教程
国内写的<Unity 3D游戏开发>. 实例挺多,对于有基础的人来说,上手会挺快的: 但进阶的东西没有涉及,可能与书的定位有关吧
- AC自动机(1)
Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). ...
- 容器--ArrayList
一.前言 作为List的重要实现之一,ArrayList已经成了我们编写程序时不可或缺的重要容器之一,面试的时候也经常会被问到,所以,深入理解其实现机制,无论是对于我们正确使用这个类来说,还是准备面试 ...
- CSS 最核心的四个概念(摘录)
本文将讲述 CSS 中最核心的几个概念,包括:盒模型.position.float等.这些是 CSS 的基础,也是最常用的几个属性,它们之间看似独立却又相辅相成.为了掌握它们,有必要写出来探讨一下,如 ...
- vmware screen
1. Question Description: the screen of the vmware looks small . 2. Solution: 2.1 look the size of sc ...
- PHP学习笔记:数据库学习心得
存储过程: 存储过程(Stored Procedure)是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库.中用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它. 因为语 ...
- 实现跨域请求jsonp方式
原理:http://madong.net.cn/index.php/2012/12/368/ 调用端: $.getJSON("http://192.168.220.85:8001/esb/a ...
- Java Map按Value排序
Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tree)的 Nav ...