转载 使用ant.jar进行文件zip压缩
来源:互联网 发布:丁威特电子狗升级软件 编辑:程序博客网 时间:2024/04/27 17:28
- 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();
- }
- }
- }
- 转载 使用ant.jar进行文件zip压缩
- 使用ant.jar进行文件zip压缩
- 使用ant实现Zip文件压缩和解压缩Zip文件
- 使用Java zip包和apach 的ant包对文件、文件夹进行压缩,兼容中文
- 使用commons-compress.jar压缩ZIP文件
- Ant 压缩打包zip文件
- java使用Apache的ant压缩和解压文件(zip)
- java 压缩解压zip文件--使用ant支持中文
- apach ant 压缩和解压缩zip文件
- 使用ant.jar实现文件压缩与解压缩
- 多文件进行zip压缩
- java 使用Apache ant.jar压缩解压zip--一种简单的实现
- 使用apache的ZipOutputStream进行zip文件压缩
- 使用apache的ZipOutputStream进行zip文件压缩
- 使用apache的ZipOutputStream进行zip文件压缩
- 使用PHP对文件进行压缩解压(zip)
- 使用zip对文件或文件夹进行压缩, 解压缩
- 使用zip对文件或文件夹进行压缩, 解压缩
- 无聊的程序员影片......
- Linux内、外部命令的使用、文件权限、文件查找(find、locate、grep)
- 调试常用的 __FILE__, __FUNCTION__, __LINE__ __DATE__, __TIME__
- UITableView默认选中一个Cell
- 关于SmartGWT
- 转载 使用ant.jar进行文件zip压缩
- IT治理——标准、框架与案例分析
- 卸载北信源内网桌面监控的完全攻略
- 学习写一个Hello World驱动
- linux grep命令
- 分享2011年12月最新的10个超酷jQuery插件
- USB设备的Bulk模式驱动程序设计
- poi 相关
- 学习编程的一些感悟