2019独角兽企业重金招聘Python工程师标准>>>

额(⊙o⊙)…

package com.ly.xjgl.utils;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;/*** ZIP工具包(支持中文)* 依赖:ant-1.9.6.jar*/
public class CHZipUtils {/**使用GBK编码可以避免压缩中文文件名乱码*/private static final String CHINESE_CHARSET = "GBK";/**文件读取缓冲区大小*/private static final int CACHE_SIZE = 1024;/*** 压缩文件* @param sourceFolder 压缩文件夹* @param zipFilePath 压缩文件输出路径*/public static void zip(String sourceFolder, String zipFilePath) {OutputStream os = null;BufferedOutputStream bos = null;ZipOutputStream zos = null;try {os = new FileOutputStream(zipFilePath);bos = new BufferedOutputStream(os);zos = new ZipOutputStream(bos);// 解决中文文件名乱码zos.setEncoding(CHINESE_CHARSET);File file = new File(sourceFolder);String basePath = null;if (file.isDirectory()) { //压缩文件夹basePath = file.getPath();} else {basePath = file.getParent();}zipFile(file, basePath, zos);} catch (Exception e) {e.printStackTrace();} finally {try {if (zos != null) {zos.closeEntry();zos.close();}if (bos != null) {bos.close();}if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}}}/*** 递归压缩文件* @param parentFile* @param basePath* @param zos* @throws Exception*/private static void zipFile(File parentFile, String basePath,ZipOutputStream zos) throws Exception {File[] files = new File[0];if (parentFile.isDirectory()) {files = parentFile.listFiles();} else {files = new File[1];files[0] = parentFile;}String pathName;InputStream is;BufferedInputStream bis;byte[] cache = new byte[CACHE_SIZE];for (File file : files) {if (file.isDirectory()) {pathName =file.getPath().substring(basePath.length() + 1) + File.separator;zos.putNextEntry(new ZipEntry(pathName));zipFile(file, basePath, zos);} else {pathName = file.getPath().substring(basePath.length() + 1);is = new FileInputStream(file);bis = new BufferedInputStream(is);zos.putNextEntry(new ZipEntry(pathName));int nRead = 0;while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {zos.write(cache, 0, nRead);}bis.close();is.close();}}}/*** 解压压缩包* @param zipFilePath 压缩文件路径* @param destDir 解压目录*/public static void unZip(String zipFilePath, String destDir) {ZipFile zipFile = null;try {BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream bos = null;zipFile = new ZipFile(zipFilePath, CHINESE_CHARSET);Enumeration<ZipEntry> zipEntries = zipFile.getEntries();File file, parentFile;ZipEntry entry;byte[] cache = new byte[CACHE_SIZE];while (zipEntries.hasMoreElements()) {entry = (ZipEntry)zipEntries.nextElement();if (entry.isDirectory()) {new File(destDir + entry.getName()).mkdirs();continue;}bis = new BufferedInputStream(zipFile.getInputStream(entry));file = new File(destDir + entry.getName());parentFile = file.getParentFile();if (parentFile != null && (!parentFile.exists())) {parentFile.mkdirs();}fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos, CACHE_SIZE);int readIndex = 0;while ((readIndex = bis.read(cache, 0, CACHE_SIZE)) != -1) {fos.write(cache, 0, readIndex);}bos.flush();bos.close();fos.close();bis.close();}} catch (IOException e) {e.printStackTrace();} finally {try {zipFile.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) throws Exception {//      String sourceFolder = "D:/test/1.txt";//      String sourceFolder = "D:/test/中文名.txt";String sourceFolder = "D:/test/cms";String zipFilePath = "D:/test/zip/压缩文件.zip";String destDir = "D:/test/zip/";CHZipUtils.zip(sourceFolder, zipFilePath);//      CHZipUtils.unZip(zipFilePath, destDir);System.out.println("********执行成功**********");}}

转载于:https://my.oschina.net/u/3646781/blog/1608039

java实现zip的压缩和解压(支持中文文件名)相关推荐

  1. java zip 高效 解压,java实现zip的压缩和解压

    [ /** * 解压缩 * @param warPath 包地址 * @param unzipPath 解压后地址 */ public static void unzip(String warPath ...

  2. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的很好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,需要导入该包. 使用zipoutputs ...

  3. .NET中zip的压缩和解压——SharpCompress

    使用Packaging无法实现通用的zip(使用其他工具压缩)的解压,只支持通过Packaging压缩包zip的解压,而SharpZipLib是基于"GPL"开源方式,风险比较大. ...

  4. java.util.zip_[Java 基础] 使用java.util.zip包压缩和解压缩文件

    Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作. Zi ...

  5. Zip文件压缩和解压功能

    1.算法基本功:递归遍历文件夹 2.学会使用Java提供的ZipFile,ZipInputStream,ZipOutputStream,CharSet(抽象类) 3.具体实现: 解压方法1: 1.利用 ...

  6. JAVA对文件进行压缩和解压

    java中经常要对一些文件压缩 java压缩文件 一 .导包 二.设置压缩文件格式 三.递归确认压缩文件 四.main方法测试 4.1创建对应文件目录1 4.1.1执行结果 4.2创建对应文件目录2 ...

  7. zip的压缩和解压命令

    以下命令均在/home目录下操作cd /home #进入/home目录 1.把/home目录下面的data目录压缩为data.zip zip -r data.zip data #压缩data目录   ...

  8. linux下zip加密压缩和解压

    对于目录a的无密码压缩: zip -r aa.zip aa 对于目录a的无密码j解压: unzip aa.zip 对于目录a的加密压缩,密码为123456: zip -rP 123456 a.zip ...

  9. java ant解压缩_java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解

    java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解 发布于 2020-4-7| 复制链接 摘记: java ant包中的org.apache.tools.zip实现 ...

最新文章

  1. 详解 Mysql LEFT JOIN和JOIN查询区别及原理
  2. CentOS安装jdk的三种方法
  3. OpenStack技术峰会PPT集萃
  4. 预览文章: c++ primer学习笔记,二:标准库类型
  5. 单元测试源码分析之二Mockito自动装配和插桩
  6. linux 记事本程序,Linux记事本操作
  7. ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自带的IOC容器)
  8. 快速理解bootstrap,bagging,boosting-三个概念
  9. 新公司要上监控,我决定用Prometheus
  10. Petya到底是个什么鬼
  11. 从零开始学WEB前端——HTML理论讲解
  12. Tomcat乱码情况完美解决
  13. 抖音的服务器究竟有多大?
  14. linux系统怎么安装office软件,如何在Linux上安装Microsoft Office | MOS86
  15. Beyond Part Models: Person Retrieval with Refined Part Pooling (and A Strong Convolutional Baseline)
  16. JSON树转换成线性列表(python)
  17. 基于QT实现的职工住房管理系统
  18. cdatatable大数据操作_C# DataRow和DataColumn:更新数据表
  19. 数据权限框架:一个实现数据权限与业务模块完全分离,让数据权限变成独立功能模块的数据权限框架
  20. linux tc 使用方法,Linux下TC使用说明

热门文章

  1. ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听
  2. CocoaPods 原理分享及遇到的问题改进
  3. 使用第三方工具覆写Object中方法
  4. 【LoadRunner】OSGI性能测试实例
  5. System.Diagnostics.Process.Start()
  6. Android测试之Monkey初体验
  7. tomcat启动卡死在: Initializing Spring root WebApplicationContext的解决办法
  8. Centos 6.8 搭建owncloud 私有云盘
  9. flex布局和单行省略不兼容
  10. EBS R12中如何使用CGI登录Form