用JAVA写一个压缩文件的例子
来源:互联网 发布:2016淘宝购物节有哪些 编辑:程序博客网 时间:2024/05/01 08:49
public class Gzip {
private static Log LOG = LogFactory.getLog(Config.class);
public final static SimpleDateFormat Date_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
/**
* @param path 要压缩的路径, 可以是目录, 也可以是文件.
* @param basePath 如果path是目录,它一般为new File(path), 作用是:使输出的zip文件以此目录为根目录, 如果为null它只压缩文件, 不解压目录.
* @param zo 压缩输出流
* @param isRecursive 是否递归
* @param isOutBlankDir 是否输出空目录, 要使输出空目录为true,同时baseFile不为null.
* @throws IOException
*/
public static void main(String[] args){
IndexerToZip("D://makeindexs","D://makeindexs","makeindexs_bak");
}
public static void zip(String path, File basePath, ZipOutputStream zo, boolean isRecursive, boolean isOutBlankDir) throws IOException {
File inFile = new File(path);
File[] files = new File[0];
if(inFile.isDirectory()) { //是目录
files = inFile.listFiles(new KeywordFileFilter(".lock",false));
} else if(inFile.isFile()) { //是文件
files = new File[1];
files[0] = inFile;
}
byte[] buf = new byte[1024];
int len;
//System.out.println("baseFile: "+baseFile.getPath());
for(int i=0; i<files.length; i++) {
String pathName = "";
if(basePath != null) {
if(basePath.isDirectory()) {
pathName = files[i].getPath().substring(basePath.getPath().length()+1);
} else {//文件
pathName = files[i].getPath().substring(basePath.getParent().length()+1);
}
} else {
pathName = files[i].getName();
}
System.out.println(pathName);
if(files[i].isDirectory()) {
if(isOutBlankDir && basePath != null) {
zo.putNextEntry(new ZipEntry(pathName+"/")); //可以使空目录也放进去
}
if(isRecursive) { //递归
zip(files[i].getPath(), basePath, zo, isRecursive, isOutBlankDir);
}
} else {
FileInputStream fin = new FileInputStream(files[i]);
zo.putNextEntry(new ZipEntry(pathName));
while((len=fin.read(buf))>0) {
zo.write(buf,0,len);
}
fin.close();
}
}
}
public static boolean IndexerToZip(String frompath,String tobasepath,String zipname)
{
if(!new File(frompath).isDirectory())return false;
OutputStream os;
try {
tobasepath = FilePath.fixPath(tobasepath);
if(!new File(tobasepath).exists())
new File(tobasepath).mkdirs();
String SavePath = new File(tobasepath).getParent() +"/"+zipname+Date_FORMAT.format(Calendar.getInstance().getTime())+".zip";
if(!new File(SavePath).exists()) new File(SavePath).createNewFile();
os = new FileOutputStream(SavePath);
BufferedOutputStream bs = new BufferedOutputStream(os);
ZipOutputStream zo = new ZipOutputStream(bs);
zip(frompath, new File(frompath), zo, true, true);
zo.closeEntry();
zo.close();
return true;
} catch (Exception e) {
LOG.debug("IndexerToZip Err:"+e.getMessage());
return false;
}
}
}
- 用JAVA写一个压缩文件的例子
- [原创]写一个仅仅压缩文件的例子
- java写的一个死锁例子
- java写一个死锁的简单例子
- 写了一个解、压缩文件的shell脚本
- JAVA的单例模式(用java写一个singleton的例子)。
- 文件DirectoryZip.java, 压缩文件例子
- 自己写的一个有序链表的例子(Java)
- 一个Java写的自定义的事件监听器例子
- JAVA反射 看视频写的一个例子
- 【面试】请写一个java死锁的例子
- 自己写的一个例子
- java的用tcp协议写的一个通信的例子
- 用java写的一个利用socket通信的C/S小例子
- java中如何写一个死锁例子
- 用composite模式写的一个二叉树的例子
- 用Swing写的一个计算器的例子(转贴)
- [转载]用servlet写的一个验证码的例子
- c/c++ 数据类型转换总结
- 什么是Web 2.0
- 图形化基因分析结果
- 关于.NET组件使用的相关资料链接,更新中...
- owc11编程的小细节备忘
- 用JAVA写一个压缩文件的例子
- delphi.net开发学习笔记(1),状态变量处理(Global.asax)
- 解决"访问IIS元数据库失败"
- 别人的SERVERLET笔记
- 如何对webbrowser和IE编程(二)
- 如何对webbrowser和IE编程(三)
- delphi.net开发学习笔记(2),Application、Session、ViewState、Cookies状态以及变量处理
- SQL 收藏------------SQL操作全集
- 如何对webbrowser和IE编程(四)