使用ant.jar进行文件zip压缩

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; /**
* 功能:
* 1 、实现把指定文件夹下的所有文件压缩为指定文件夹下指定 zip 文件
* 2 、实现把指定文件夹下的 zip 文件解压到指定目录下
*/ public class ZipUtils { public static void main(String[] args) { zip ("D:\\zip测试", "D:\\测试结果.zip"); unZip("D:\\测试结果.zip", "D:\\解压结果"); } /**
* 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件
* @param sourceDir
* @param zipFile
*/ public static void zip(String sourceDir, String zipFile) { OutputStream os; try { os = new FileOutputStream(zipFile); BufferedOutputStream bos = new BufferedOutputStream(os); ZipOutputStream zos = new ZipOutputStream(bos); File file = new File(sourceDir); String basePath = null; if (file.isDirectory()) { basePath = file.getPath(); } else {//直接压缩单个文件时,取父目录 basePath = file.getParent(); } zipFile(file, basePath, zos); zos.closeEntry(); zos.close(); } catch (Exception e) { e.printStackTrace(); } } /**
* 功能:执行文件压缩成zip文件
* @param source
* @param basePath 待压缩文件根目录
* @param zos
*/ private static void zipFile(File source, String basePath, ZipOutputStream zos) { File[] files = new File[0]; if (source.isDirectory()) { files = source.listFiles(); } else { files = new File[1]; files[0] = source; } String pathName;//存相对路径(相对于待压缩的根目录) byte[] buf = new byte[1024]; int length = 0; try { for (File file : files) { if (file.isDirectory()) { pathName = file.getPath().substring(basePath.length() + 1) + "/"; zos.putNextEntry(new ZipEntry(pathName)); zipFile(file, basePath, zos); } else { pathName = file.getPath().substring(basePath.length() + 1); InputStream is = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(is); zos.putNextEntry(new ZipEntry(pathName)); while ((length = bis.read(buf)) > 0) { zos.write(buf, 0, length); } is.close(); } } } catch (Exception e) { e.printStackTrace(); } } /**
* 功能:解压 zip 文件,只能解压 zip 文件
* @param zipfile
* @param destDir
*/ public static void unZip(String zipfile, String destDir) { destDir = destDir.endsWith("\\") ? destDir : destDir + "\\"; byte b[] = new byte[1024]; int length; ZipFile zipFile; try { zipFile = new ZipFile(new File(zipfile)); Enumeration enumeration = zipFile.getEntries(); ZipEntry zipEntry = null; while (enumeration.hasMoreElements()) { zipEntry = (ZipEntry) enumeration.nextElement(); File loadFile = new File(destDir + zipEntry.getName()); if (zipEntry.isDirectory()) { loadFile.mkdirs(); } else { if (!loadFile.getParentFile().exists()){ loadFile.getParentFile().mkdirs(); } OutputStream outputStream = new FileOutputStream(loadFile); InputStream inputStream = zipFile.getInputStream(zipEntry); while ((length = inputStream.read(b)) > 0) outputStream.write(b, 0, length); } } } catch (IOException e) { e.printStackTrace(); } } }

读取zip文件,不解压缩直接解析,支持文件名中文,解决内容乱码的更多相关文章

  1. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

  2. java 读取Zip文件进行写入

    直接读取ZIp文件读取写入到别的文件中. package jp.co.misumi.mdm.batch; import java.io.BufferedReader; import java.io.F ...

  3. PHP生成PDF完美支持中文,解决TCPDF乱码

    PHP生成PDF完美支持中文,解决TCPDF乱码 2011-09-26 09:04 418人阅读 评论(0) 收藏 举报 phpfontsheaderttfxhtml文档 PHP生成PDF完美支持中文 ...

  4. java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式

    代码如下: package com.cn.util; import java.io.BufferedInputStream; import java.io.File; import java.io.F ...

  5. Silverlight读取Zip文件中的图片与视频

    首先看看Demo的截图: 下面我将一步步展示实现这个Demo的过程,这个需求就是读出Zip文件中的图片与视频. Demo整体架构: 首先我们准备几张图片和视频,然后将其压缩至resource.zip文 ...

  6. php读取zip文件(删除文件,提取文件,增加文件)实例

    <?php /* php 从zip压缩文件中提取文件 */ $zip = new ZipArchive; if ($zip->open('jQuery五屏上下滚动焦点图代码.zip') = ...

  7. cocos2d-x 读取 json 文件并用 jsoncpp 做解析

    一码胜万言(请看注释) CclUtil.h // // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #if ...

  8. Python 标准库 —— zipfile(读取 zip 文件)

    Python模块学习:zipfile zip文件操作 Python 学习入门(16)-- zipfile 0. 解压 with zipfile.ZipFile('../data/jaychou_lyr ...

  9. 读取Zip文件内容

    第一步,上次文件并保存到服务器目录下 /// <summary> /// 上传压缩文件 /// </summary> protected void UploadZip() { ...

随机推荐

  1. 从reduce函数说起...

    reduce函数:  方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值, 最终返回的要看函数内部return的内容. 1. 累加器: var ar ...

  2. hdu 5391 Zball in Tina Town 威尔逊定理 数学

    Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  3. ZC_03_创建对象

    1. 正如 上一篇文章中所见,反射创建 类实例的方式,主要为2类: (1).Class对象.newInstance() 这是使用 默认的无参构造函数 创建对象 (2).Constructor对象.ne ...

  4. TUNING FOR ALL FLASH DEPLOYMENTS

    Ceph Tuning and Best Practices for All Flash Intel® Xeon® ServersLast updated: January 2017 TABLE OF ...

  5. linux 里rpm包到底是干什么用的

    Linux RPM全称是“RedHat Package Manager”,最早是Red Hat公司开发的,后来在CentOS.Fedora.SUSE都用它.而rpm包则是软件编译完成后按照RPM机制打 ...

  6. gulp 相关文章

    1.https://www.cnblogs.com/sxz2008/p/6370221.html 2.https://www.cnblogs.com/wujie520303/p/4964931.htm ...

  7. ionic2——开发利器之Visual Studio Code 常用插件整理

    1.VsCode官方插件地址: http://code.visualstudio.com/docs 2.使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件 添加方法使 ...

  8. springboot项目执行controller方法时进入慢的问题

    今天在部署springboot项目到阿里云时,出现登录方法执行特别慢的问题.刚开始以为是卡死了,等了3,4分钟才进去,最后会出现如下信息: 2018-01-28 15:38:36.958 INFO 4 ...

  9. 剑指offer--32.把数组排成最小的数

    用to_string()将整形转化为字符串,对字符串进行比较 --------------------------------------------------------------------- ...

  10. jquery 中多条件选择器,相对选择器,层次选择器的区别

    一.Jquery常用的过滤选择器如下所示: 1.:first,选取第一个元素,比如$("div:first")选取第一个div元素 2.:last,选取最后一个元素,比如$(&qu ...