java实现文件及目录压缩
package org.alfresco.repo.bom.util; 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 org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream; /**
* Compressor Util
* @author HJ
*
*/
public class CompressorUtil { private static final String source = "F:/test"; // wait compressor source path
private static final String zipSource = "F:/chiang.zip"; // after compressor zip file path
private static long startTime;// compressor start system time
private static long endTime;// compressor end system time public void compressor() throws Exception{
startTime = System.currentTimeMillis();//record start compressor system time ,
boolean flag = false;// flag :true->compressor success
String baseDir = "";//defalut relative Dir , "" is gen Dir File s = new File(source);
File zs = new File(zipSource);//create zip file
if (zs.exists()) {// if this dir exists this zip file
zs.delete(); // delete this zip file ,
}
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zs));
zos.setEncoding("GBK"); // solve Chinese garbled
startCompressor(baseDir, zos, s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (zos!=null)
zos.close();
endTime = System.currentTimeMillis();
System.out.println("compressor success,use time:"+(endTime-startTime)+"ms");
}
} public void startCompressor(String baseDir,ZipOutputStream zos,File source) throws Exception{
if (source.isFile()) {// is file
toCompressedFile(baseDir, zos, source);
}
if (source.isDirectory()) { //is dir
File[] sources = source.listFiles(); // get dir all files ( file or dir)
for(File f:sources){
if (f.isFile()) {// is file
toCompressedFile(baseDir, zos, f);
}
if (f.isDirectory()) {//is dir
// if is dir , update baseDir value .
String newBaseDir = baseDir + f.getName() + "/";
createCompressedDir(baseDir, zos, f);//create dir and entry
startCompressor(newBaseDir, zos, f); // Re
}
}
}
}
/**
* add entry to zip file by stream way
* @param baseDir
* @param zos
* @param f
* @throws Exception
*/
public void toCompressedFile(String baseDir,ZipOutputStream zos,File f) throws Exception{
InputStream input = null;
ZipEntry z = new ZipEntry(baseDir+f.getName());
try {
zos.putNextEntry(z); // add entry to zip file
input = new FileInputStream(f);
int data = 0;
while ((data=input.read())!=-1) {
zos.write(data);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(input!=null)
input.close();
zos.closeEntry();
}
} /**
* create compressed file dir and ZipEntry
* @param baseDir
* @param zos zip file's ZipOutputStream
* @param f
*/
public void createCompressedDir(String baseDir,ZipOutputStream zos,File f){
ZipEntry z = new ZipEntry(baseDir+f.getName()+"/");
try {
zos.putNextEntry(z);
zos.closeEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //test main method
public static void main(String[] args) throws Exception{
CompressorUtil cu = new CompressorUtil();
cu.compressor();
}
}
java实现文件及目录压缩的更多相关文章
- java创建文件和目录
java创建文件和目录 2013-09-04 12:56 99933人阅读 评论(7) 收藏 举报 分类: JAVA基础(10) 版权声明:本文为博主原创文章,未经博主允许不得转载. 创建文件和目 ...
- 封装7z软件实现批量文件或目录压缩
哈哈,作为一个特别懒的运维人来说 兄弟我写了一个批量压缩文件或目录的小工具,用来批量压缩文件目录 弄一下,然后就不用管他了,后天看结果就好了 操作步骤: 1.选择想做压缩处理的根目录 2.选择你要的功 ...
- Java删除文件或目录及目录下所有文件
一直在做C++相关开发的工作.突然某一天一时兴起,想学习下Java开发.然后再网上找到一本Java简明教程,入门是够用了.看到文件IO这一章,想起之前用C++做的删除文件或目录的练习,于是打算用Jav ...
- java删除文件及其目录
1.删除指定文件路径 public @ResponseBody String deleteFiles(HttpServletRequest request) { log.info(this.getCl ...
- learning java 访问文件和目录
import java.io.File; import java.io.IOException; public class FileTest { public static void main(Str ...
- java下载文件指定目录下的文件
方法一: @RequestMapping('download')def download(HttpServletRequest request, HttpServletResponse respons ...
- 【java工具类】删除文件及目录
FileUtil.java /** * 删除文件及目录 * @param file; */ public static boolean delFile(File file) { if (!file.e ...
- Shell命令-文件及目录操作之chattr、lsattr
文件及目录操作 - chattr.lsattr 1. chattr:改变文件属性 chattr命令的功能说明 chattr命令用于改变文件属性.这项指令可改变存放在ext2文件系统上的文件或目录属性, ...
- JAVA 实现将多目录多层级文件打成ZIP包后保留层级目录下载 ZIP压缩 下载
将文件夹保留目录打包为 ZIP 压缩包并下载 上周做了一个需求,要求将数据库保存的 html 界面取出后将服务器下的css和js文件一起打包压缩为ZIP文件,返回给前台:在数据库中保存的是html标签 ...
随机推荐
- Redis开启持久化
配置appendonly 打开redis.conf找到appendonly. 将 appendonly no 改为 appendonly yes 配置appendfsync appendfsync a ...
- Open CV 图像显示(1)
演示:读入一张图片,并显示 #include "stdafx.h" #include <opencv2/core/core.hpp> #include ...
- ural 1252. Sorting the Tombstones
1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...
- 移动端 设计与开发经验之ViewPort
Viewport :字面意思为视图窗口,在移动 web 开发中使用.表示将设备浏览器宽度虚拟成一个特定的值(或计算得出),这样利于移动 web 站点跨设备显示效果基本一致. 基本写法: <met ...
- Kosaraju 算法
Kosaraju 算法 一.算法简介 在计算科学中,Kosaraju的算法(又称为–Sharir Kosaraju算法)是一个线性时间(linear time)算法找到的有向图的强连通分量.它利用了一 ...
- tornado 学习笔记3 安装
3 安装 安装分为两种方式:自动安装和手动安装,推荐采用自动安装,提前是机器联网的情况下. 3.1 自动安装: 命令:pip install tornado 写一段代码测试一下安装是否成功 # -*- ...
- 【BZOJ3207】花神的嘲讽计划I 可持久化线段树/莫队
看到题目就可以想到hash 然后很自然的联想到可持久化权值线段树 WA:base取了偶数 这道题还可以用莫队做,比线段树快一些 可持久化线段树: #include<bits/stdc++.h&g ...
- ArcGIS几种数据格式
ArcGIS几种数据格式 ArcInfo常用以下格式的数据:shp.Coverage..Raster CAD和Geodatabase.各种数据的组织形式不一样,其中shp.Coverage.Raste ...
- python 之Twsited
Twisted是一个事件驱动的网络框架,其中包含了诸多功能,例如网络协议,线程,数据库管理,网络操作,电子邮件等 事件驱动 一,注册事件 二,触发事件 自定义事件框架 event_fram.py # ...
- 索引器、哈希表Hashtabl、字典Dictionary(转)
一.索引器 索引器类似于属性,不同之处在于它们的get访问器采用参数.要声明类或结构上的索引器,使用this关键字. 示例: 索引器示例代码 /// <summary> /// 存储星 ...