网上有关压缩和解压zip包的博文一大堆,我随便找了一个。看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正:

package com.wangpeng.utill;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; /**
* @author wangpeng
*/
public class ToolOfZip { /**
* 解压zip包
* @param inputPath 被解压的zip包的路径
* @param targetPath 被解压到的文件夹
* @param isClear 是否清楚已有文件夹
*/
public static void unZip(String inputPath,String targetPath, boolean isClear) {
try {
if (null == targetPath || "".equals(targetPath)) {
targetPath = inputPath.substring(0,inputPath.lastIndexOf("."));
} ZipFile zipFile = new ZipFile(new File(inputPath));
Enumeration<? extends ZipEntry> entrys = zipFile.entries(); while (entrys.hasMoreElements()) {
ZipEntry entry = entrys.nextElement();
String name = entry.getName();// aaa\testdir\xxxxx\eee.txt
String strTargetPath=targetPath+"/"+name;
String strTargetDir=strTargetPath.substring(0, strTargetPath.lastIndexOf(File.separator));
if (entry.isDirectory()) {
File dir = new File(strTargetPath);
if (!dir.exists()) {
dir.mkdirs();
}
} else {
File dir = new File(strTargetDir);
if (!dir.exists()) {
dir.mkdirs();
} File file = new File(strTargetPath);
if (file.exists() && isClear) {
file.delete();
} if (!file.exists()) {
InputStream in = zipFile.getInputStream(entry);
ToolOfFile.copyFile(in, file.getAbsolutePath());
}
}
}
} catch (ZipException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 创建ZIP文件
*
* @param sourcePath
* 文件或文件夹路径
* @param zipPath
* 生成的zip文件存在路径(包含文件名称)
*/
public static void createZip(String sourcePath, String zipPath) {
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipPath);
zos = new ZipOutputStream(fos);
writeZip(new File(sourcePath), "", zos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} private static void writeZip(File inFile, String parentPath,
ZipOutputStream zipOutStream) {
if (inFile.exists()) {
if (inFile.isDirectory()) {
parentPath += inFile.getName() + File.separator;
File[] files = inFile.listFiles();
for (File file : files) {
writeZip(file, parentPath, zipOutStream);
}
} else {
FileInputStream fileInStream = null;
try {
fileInStream = new FileInputStream(inFile);
ZipEntry entry = new ZipEntry(parentPath + inFile.getName());
zipOutStream.putNextEntry(entry); byte[] buff = new byte[1024];
int len;
while ((len = fileInStream.read(buff)) != -1) {
zipOutStream.write(buff, 0, len);
zipOutStream.flush();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInStream != null) {
fileInStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} public static void main(String[] args) {
String oldPath = "F:/test/ddd/";
String newPath = "F:/test/ccc.zip";
// ToolOfZip.createZip(oldPath, newPath);
//ToolOfZip.unZip(newPath, null, false);
System.out.println();
System.out.println("---------ok----------");
}
}

java 压缩和解压zip包的更多相关文章

  1. 使用PowerShell压缩和解压ZIP包

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月13日. 解压ZIP包 使用PowerShell的Expand-Archive命令.PowerShell官方文档地址. 命令格式: ...

  2. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  3. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  4. JAVA调用外部安装7-Zip压缩和解压zip文件

    1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /**      * 生成.zip压缩文件      * @param fi ...

  5. java压缩和解压字符串,Byte数组,String

    在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...

  6. Qt压缩和解压 zip

    zlib编译详见 https://blog.csdn.net/zhangxuechao_/article/details/85049711 下载quazip https://github.com/st ...

  7. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  8. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  9. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

随机推荐

  1. poj 1556(迪杰斯特拉+计算几何)

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7641   Accepted: 2987 Descrip ...

  2. jquerycheckbox事件

    https://stackoverflow.com/questions/7031226/jquery-checkbox-change-and-click-event $(document).ready ...

  3. UpdateData、Invalidate、InvalidateRect和UpdateWindow及RedrawWindow

    Invalidate 在消息队列中加入一条WM_PAINT消息,其无效区为整个客户区. 窗口的客户区无效意味着需要重绘.例如,如果一个被其它窗口遮住的窗口变成了前台窗口,那么原来被遮住的部分就是无效的 ...

  4. win上配置nginx

    win上配置nginx 网上配置nginx的教程大多都是linux上的,今天贴出来nginx在win上的配置,在此篇配置中,nginx代理了Tomcat以及node服务.配置如下: 注意:根据实际经验 ...

  5. POJ 1163.The Triangle-动态规划

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50122   Accepted: 30285 De ...

  6. Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离

    C. Watchmen   time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  7. CF 1005C Summarize to the Power of Two 【hash/STL-map】

    A sequence a1,a2,-,an is called good if, for each element ai, there exists an element aj (i≠j) such ...

  8. Android学习--探究服务(一)

    什么是服务? 服务(service)是Android中实现程序后台运行的解决方案,它非常适合去执行那些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖任何的用户界面,即使应用被切换到后台或者 ...

  9. 17、Django实战第17天:机构详情展示

    1.进入xadmin添加测试数据(教师.课程) 2.把以下4个前端页面复制到templates中 先打开这几个页面分析,它们和之前的课程机构列表页是不一样的机构,且没有共同的部分,但是这4个页面却是类 ...

  10. 【容斥原理】CDOJ - 1544 - 当咸鱼也要按照基本法

    众所周知zhu是一个大厨,zhu一直有自己独特的咸鱼制作技巧. tang是一个咸鱼供应商,他告诉zhu在他那里面有NN条咸鱼(标号从1到N)可以被用来制作. 每条咸鱼都有一个咸鱼值KiKi,初始时所有 ...