本站首页    管理页面    写新日志    退出


«June 2026»
123456
78910111213
14151617181920
21222324252627
282930


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:天地无用
日志总数:55
评论数量:43
留言数量:1
访问次数:197934
建立时间:2008年4月17日




如何用Java压缩文件(转)
软件技术

kkk888929 发表于 2008/4/22 14:56:08

/** [Demo Program]** try {* zipDir("c:/temp", "c:/temp.zip", true);* } catch(Exception e) {* //handle exception* }*/public static void zipDir(String directory, String zipname, booleanoverride) {try {File file = new File(zipname);if (file.isDirectory())return;if ((!override) && file.exists())return;file.delete();ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));zipDir(directory, zos);DgIOUtils.close(zos);} catch (FileNotFoundException e) {}}protected static boolean zipDir(String directory, ZipOutputStream zos) {try {//create a new File object based on the directory we have to zip FileFile zipDir = new File(directory);//get a listing of the directory contentString[] dirList = zipDir.list();byte[] readBuffer = new byte[2156];int bytesIn = 0;//loop through dirList, and zip the filesfor (int i = 0; i < dirList.length; i++) {File f = new File(zipDir, dirList[i]);if (f.isDirectory()) {//if the File object is a directory, call this//function again to add its content recursivelyString filePath = f.getPath();zipDir(filePath, zos);//loop againcontinue;}//if we reached here, the File object f was not a directory//create a FileInputStream on top of fFileInputStream fis = new FileInputStream(f);//create a new zip entryZipEntry anEntry = new ZipEntry(f.getPath());//place the zip entry in the ZipOutputStream objectzos.putNextEntry(anEntry);//now write the content of the file to the ZipOutputStreamwhile ((bytesIn = fis.read(readBuffer)) != -1) {zos.write(readBuffer, 0, bytesIn);}//close the StreamDgIOUtils.close(fis);return true;}} catch (Exception e) {//handle exception}return false;}


阅读全文(2818) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.018 second(s), page refreshed 144837350 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号