来源https://www.open-open.com/lib/view/open1363592512046.html

被zip 折腾的要死,特意记录一下来,以后防翻车

package cn.com.servyou.nbxxh.wxadmin.common.utils.file;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 org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;import de.innosystec.unrar.Archive;
public class Test {private static final int BUFFEREDSIZE = 1024;public Test() {// TODO Auto-generated constructor stub}/*** 解压zip格式的压缩文件到当前文件夹** @param zipFileName* @throws Exception*/@SuppressWarnings("unchecked")public synchronized void unzipFile(String zipFileName) throws Exception {try {File f = new File(zipFileName);ZipFile zipFile = new ZipFile(zipFileName);if ((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");}String strPath, gbkPath, strtemp;File tempFile = new File(f.getParent());strPath = tempFile.getAbsolutePath();java.util.Enumeration e = zipFile.getEntries();while (e.hasMoreElements()) {org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();gbkPath = zipEnt.getName();if (zipEnt.isDirectory()) {strtemp = strPath + "/" + gbkPath;File dir = new File(strtemp);dir.mkdirs();continue;} else {//读写文件InputStream is = zipFile.getInputStream(zipEnt);BufferedInputStream bis = new BufferedInputStream(is);gbkPath = zipEnt.getName();strtemp = strPath + "/" + gbkPath;//建目录String strsubdir = gbkPath;for (int i = 0; i < strsubdir.length(); i++) {if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {String temp = strPath + "/" + strsubdir.substring(0, i);File subdir = new File(temp);if (!subdir.exists())subdir.mkdir();}}FileOutputStream fos = new FileOutputStream(strtemp);BufferedOutputStream bos = new BufferedOutputStream(fos);int c;while ((c = bis.read()) != -1) {bos.write((byte) c);}bos.close();fos.close();}}} catch (Exception e) {e.printStackTrace();throw e;}}/*** 解压zip格式的压缩文件到指定位置** @param zipFileName 压缩文件* @param extPlace    解压目录* @throws Exception*/@SuppressWarnings("unchecked")public synchronized void unzip(String zipFileName, String extPlace) throws Exception {try {(new File(extPlace)).mkdirs();File f = new File(zipFileName);ZipFile zipFile = new ZipFile(zipFileName);if ((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");}String strPath, gbkPath, strtemp;File tempFile = new File(extPlace);strPath = tempFile.getAbsolutePath();java.util.Enumeration e = zipFile.getEntries();while (e.hasMoreElements()) {org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();gbkPath = zipEnt.getName();if (zipEnt.isDirectory()) {strtemp = strPath + File.separator + gbkPath;File dir = new File(strtemp);dir.mkdirs();continue;} else {//读写文件InputStream is = zipFile.getInputStream(zipEnt);BufferedInputStream bis = new BufferedInputStream(is);gbkPath = zipEnt.getName();strtemp = strPath + File.separator + gbkPath;//建目录String strsubdir = gbkPath;for (int i = 0; i < strsubdir.length(); i++) {if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {String temp = strPath + File.separator + strsubdir.substring(0, i);File subdir = new File(temp);if (!subdir.exists())subdir.mkdir();}}FileOutputStream fos = new FileOutputStream(strtemp);BufferedOutputStream bos = new BufferedOutputStream(fos);int c;while ((c = bis.read()) != -1) {bos.write((byte) c);}bos.close();fos.close();}}} catch (Exception e) {e.printStackTrace();throw e;}}/*** 解压zip格式的压缩文件到指定位置** @param zipFileName 压缩文件* @param extPlace    解压目录* @throws Exception*/@SuppressWarnings("unchecked")public synchronized void unzip(String zipFileName, String extPlace, boolean whether) throws Exception {try {(new File(extPlace)).mkdirs();File f = new File(zipFileName);ZipFile zipFile = new ZipFile(zipFileName);if ((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");}String strPath, gbkPath, strtemp;File tempFile = new File(extPlace);strPath = tempFile.getAbsolutePath();java.util.Enumeration e = zipFile.getEntries();while (e.hasMoreElements()) {org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();gbkPath = zipEnt.getName();if (zipEnt.isDirectory()) {strtemp = strPath + File.separator + gbkPath;File dir = new File(strtemp);dir.mkdirs();continue;} else {//读写文件InputStream is = zipFile.getInputStream(zipEnt);BufferedInputStream bis = new BufferedInputStream(is);gbkPath = zipEnt.getName();strtemp = strPath + File.separator + gbkPath;//建目录String strsubdir = gbkPath;for (int i = 0; i < strsubdir.length(); i++) {if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {String temp = strPath + File.separator + strsubdir.substring(0, i);File subdir = new File(temp);if (!subdir.exists())subdir.mkdir();}}FileOutputStream fos = new FileOutputStream(strtemp);BufferedOutputStream bos = new BufferedOutputStream(fos);int c;while ((c = bis.read()) != -1) {bos.write((byte) c);}bos.close();fos.close();}}} catch (Exception e) {e.printStackTrace();throw e;}}/*** 压缩zip格式的压缩文件** @param inputFilename 压缩的文件或文件夹及详细路径* @param zipFilename   输出文件名称及详细路径* @throws IOException*/public synchronized void zip(String inputFilename, String zipFilename) throws IOException {zip(new File(inputFilename), zipFilename);}/*** 压缩zip格式的压缩文件** @param inputFile   需压缩文件* @param zipFilename 输出文件及详细路径* @throws IOException*/public synchronized void zip(File inputFile, String zipFilename) throws IOException {ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));try {zip(inputFile, out, "");} catch (IOException e) {throw e;} finally {out.close();}}/*** 压缩zip格式的压缩文件** @param inputFile 需压缩文件* @param out       输出压缩文件* @param base      结束标识* @throws IOException*/@SuppressWarnings("unused")private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {if (inputFile.isDirectory()) {File[] inputFiles = inputFile.listFiles();out.putNextEntry(new ZipEntry(base + "/"));base = base.length() == 0 ? "" : base + "/";for (int i = 0; i < inputFiles.length; i++) {zip(inputFiles[i], out, base + inputFiles[i].getName());}} else {if (base.length() > 0) {out.putNextEntry(new ZipEntry(base));} else {out.putNextEntry(new ZipEntry(inputFile.getName()));}FileInputStream in = new FileInputStream(inputFile);try {int c;byte[] by = new byte[BUFFEREDSIZE];while ((c = in.read(by)) != -1) {out.write(by, 0, c);}} catch (IOException e) {throw e;} finally {in.close();}}}/*** 解压tar格式的压缩文件到指定目录下** @param tarFileName 压缩文件* @param extPlace    解压目录* @throws Exception*/public synchronized void untar(String tarFileName, String extPlace) throws Exception {}/*** 压缩tar格式的压缩文件** @param inputFilename 压缩文件* @param tarFilename   输出路径* @throws IOException*/public synchronized void tar(String inputFilename, String tarFilename) throws IOException {tar(new File(inputFilename), tarFilename);}/*** 压缩tar格式的压缩文件** @param inputFile   压缩文件* @param tarFilename 输出路径* @throws IOException*/public synchronized void tar(File inputFile, String tarFilename) throws IOException {TarOutputStream out = new TarOutputStream(new FileOutputStream(tarFilename));try {tar(inputFile, out, "");} catch (IOException e) {throw e;} finally {out.close();}}/*** 压缩tar格式的压缩文件** @param inputFile 压缩文件* @param out       输出文件* @param base      结束标识* @throws IOException*/@SuppressWarnings("unused")private synchronized void tar(File inputFile, TarOutputStream out, String base) throws IOException {if (inputFile.isDirectory()) {File[] inputFiles = inputFile.listFiles();out.putNextEntry(new TarEntry(base + "/"));base = base.length() == 0 ? "" : base + "/";for (int i = 0; i < inputFiles.length; i++) {tar(inputFiles[i], out, base + inputFiles[i].getName());}} else {if (base.length() > 0) {out.putNextEntry(new TarEntry(base));} else {out.putNextEntry(new TarEntry(inputFile.getName()));}FileInputStream in = new FileInputStream(inputFile);try {int c;byte[] by = new byte[BUFFEREDSIZE];while ((c = in.read(by)) != -1) {out.write(by, 0, c);}} catch (IOException e) {throw e;} finally {in.close();}}}/* <!-- ant -->maven 坐标<dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.9.7</version></dependency>*/}

java压缩解压缩rar、zip文件相关推荐

  1. java 中导出word后压缩文件_Java批量导出word压缩后的zip文件案例

    一.js代码,由于参数比较大所以利用form表单使用post导出 function export_word(){ var selectedRows = $("#dg").datag ...

  2. linux 压缩文件夹rar,让Ubuntu 16.04可以压缩/解压缩RAR文件

    在Ubuntu 16.04下经常要用到压缩/解压缩RAR文件, 每次都是网上搜索教程,而且都没有详细解析每个命令的具体用法,现在详细记下方法,以备再次用的时候方便的找到. 因为 参数 e 和 x 经常 ...

  3. vb.net 解压缩(文件/文件夹解压缩,zip文件浏览,单文件解压,分卷压缩)

    vb.net 解压缩(文件/文件夹解压缩,zip文件浏览,单文件解压,分卷压缩) DLL:Ionic.Zip.dll 说明: 1.解压/压缩 显示加密进度及总进度. 2.zip文件打开,浏览内部文件. ...

  4. android zip格式应用,Android 压缩解压zip文件

    Android 压缩解压zip文件 上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩 ...

  5. 解压 rar,zip 文件保存到本地

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.导入依赖 二.功能代码 三.测试结果 解压前 解压后 总结 参考博客1 参考博客2 参考博客3(此方法未使用) 前 ...

  6. RAR/ZIP文件解压(兼容RAR5)

    前言: 记录压缩包解压功能开发过程遇见的一些问题,及最终的解决方案: 原始需求: 客户提出需要批量上传文档,上传文件为包含一系列文件的压缩包,格式为zip或rar: 历史实现方式: zip格式:使用n ...

  7. 压缩出来的zip文件带有密码?如何取消zip密码?

    一般来讲,压缩文件之后是不会带有密码,除非是我们自己在压缩文件的过程中设置了密码. 有些小伙伴在这里需也会提出疑问,在压缩过程中也没有添加密码,就和普通压缩过程一样,但是解压压缩好的zip文件就会被提 ...

  8. 使用InfoZip压缩解压zip文件

    使用InfoZip压缩解压zip文件 源码下载地址:http://download.csdn.net/detail/risingsun001/4254821 先贴部分源码: void CInfozip ...

  9. java 压缩加密_java中文件如何加密压缩?

    @ 文件加密压缩 在现如今已经可以实现大部分功能的互联网需求中,安全成为了需要的非必需品,工作需求中会遇到对导出的 excel 报表 做加密压缩的问题,今天有时间总结一下,我这里简单列举功能代码,连带 ...

最新文章

  1. java磁盘读写b 树_原来你是这样的B+树
  2. ML之FE:特征工程中常用的一些处理手段(缺失值填充、异常值检测等)及其对应的底层代码的实现
  3. 合肥.NET俱乐部第二期技术沙龙活动预告
  4. 无处不在_Java无处不在:使用DukeScript在任何地方运行一次编写
  5. Linux实现免密码登录
  6. C# Monitor实现
  7. 点到曲线的距离公式_推导点到直线的距离公式到底有多少种方法?
  8. 【es】使用ElasticSearch的44条建议 性能优化
  9. 论文赏析[NAACL19]无监督循环神经网络文法 (URNNG)
  10. PHP 删除文件,文件下的目录
  11. OCP题库062新出现的考试题-第23题
  12. H.264/AVC的编码格式
  13. matlab中的常用符号,matlab特殊符号表
  14. Chrome打开网页不慢,但是卡顿,滚动滚轮卡顿,打开b站等特定网址卡顿问题解决
  15. 带宽、延时、吞吐率、PPS 这些都是啥?
  16. 核芯基站_更新CLE版本
  17. Telegram Download Default Chat Wallpaper
  18. Web服务器群集——编译安装Nginx-1.16及虚拟主机配置
  19. RGB与16进制颜色值互相转换
  20. Spark SQL 内置函数(五)Aggregate Functions(基于 Spark 3.2.0)

热门文章

  1. 怎么做好网络营销推广引流客户?
  2. Flutter环境配置基本语法如何使用库
  3. Docker 入门教程(一) - Docker Tutorial
  4. PHP之两个日期之间相差天数
  5. Python中%是什么意思?如何使用?
  6. android自动生成dimens适配文件
  7. 2005至2022历史路网下载及分析
  8. C语言beep中有什么作用,C++中发声函数Beep用法
  9. Kafka入门教程及安装
  10. 40 岁编程经验 30 年!支付宝资深工程师的程序人生