代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
一个压缩解压缩文件和目录的完整程序
作者:
/ 发布于
2011/1/14
/
836
<div>// 一个压缩解压缩文件和目录的完整程序. ... // 版权没有, 随便使用. package cross.pauliuyou.tool.io; import java.io.*; import java.util.*; import java.util.zip.*; public class ZipUtil { public static int bufsize = 8192; private String zipBase; private int maxFileNameLength; private long totalLength; private long bytesHandled; private String getSpace(int num) { String space = ""; for (int i = 0; i < num; i++) { space += " "; } return space; } private void getLength(File path) { if (path.isFile()) { totalLength += path.length(); if (path.getPath().length() > maxFileNameLength) { maxFileNameLength = path.getPath().length(); } } else { File [] fs = path.listFiles(); for (int i = 0; i < fs.length; i++) { getLength(fs[i]); } } } private String lengthString(long fileLength) { long newValue = 0; String size = null; if (fileLength < 1024) { size = fileLength + " B"; } else if (fileLength < (1024 * 1024)) { newValue = fileLength / 1024; size = newValue + " KB"; } else if (fileLength < (1024 * 1024 * 1024)) { newValue = fileLength / (1024 * 1024); size = newValue + " MB"; } else { newValue = fileLength / (1024 * 1024 * 1024); size = newValue + " GB"; } return size; } public void zip(String path) throws Exception { zip(path,null); } public void zip(String path,String dest) throws Exception { File f = new File(path); if (!f.exists()) { System.out.println("File : " + path + " Not Exists!"); return; } getLength(f); System.out.println("total " + lengthString(totalLength) + " to zip"); String path2 = ""; for (int i = 0; i < path.length(); i++) { char c = path.charAt(i); if (c == '\\') { c = '/'; } path2 += c; } path = path2; zipBase = path.substring(path.lastIndexOf("/") == -1 ? 0 : path.lastIndexOf("/") + 1); if (dest == null) { if (f.isFile()) { if (dest == null) { zipBase = "./"; dest = path.substring(0,(path.lastIndexOf(".") == -1 ? path.length() : path.lastIndexOf("."))) + ".zip"; } } else { path2 = path.substring(0,path.lastIndexOf("/") == -1 ? path.length() : path.lastIndexOf("/")); if (path.equals(path2)) { dest = "."; } dest = path2 + "/" + zipBase.substring(0,(zipBase.lastIndexOf(".") == -1 ? zipBase.length() : zipBase.lastIndexOf("."))) + ".zip"; } } ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(dest)); zipFile(f,zipOut); zipOut.close(); System.out.print("\r100%\t"); System.out.println(path + " has been compressed to zip file : " + dest + getSpace(40)); } public void zipFile(File f,ZipOutputStream zipOut) throws Exception { if (f.isDirectory()) { File [] fs = f.listFiles(); for (int i = 0; i < fs.length; i++) { zipFile(fs[i],zipOut); } } else { String path = f.getPath(); FileInputStream fileIn = new FileInputStream(path); String entryName = path; int basePos = path.indexOf(zipBase); if (basePos > 0) { entryName = path.substring(basePos); } ZipEntry theEntry = new ZipEntry(entryName); theEntry.setSize(f.length()); zipOut.putNextEntry(theEntry); byte[] buffer = new byte[bufsize]; int bytesRead = fileIn.read(buffer,0,bufsize); bytesHandled += bytesRead; while (bytesRead > 0) { zipOut.write(buffer,0,bytesRead); bytesRead = fileIn.read(buffer,0,bufsize); bytesHandled += bytesRead; int percent = (int)(((double)bytesHandled / totalLength) * 100 ); System.out.print("\r" + percent + "% "); if (totalLength > 1024 * 1024 * 80) { int j; for (j = 0; j < percent % 4 + 1; j++) { System.out.print(">"); } for (int x = 0; x < 4 - j; x++) { System.out.print(" "); } } System.out.print(" Compress File " + path + getSpace(maxFileNameLength - path.length())); } fileIn.close(); zipOut.closeEntry(); } } public void unzip(String zipFileName) throws Exception { unzip(zipFileName,null); } public void unzip(String zipFileName,String dest) throws Exception { File f = new File(zipFileName); if (!f.exists()) { System.out.println("Zip File : " + zipFileName + " Not Exists!"); return; } byte[] buffer = new byte[bufsize]; ZipFile zf = new ZipFile(zipFileName); ZipEntry theEntry = null; Enumeration enumer = zf.entries(); while (enumer.hasMoreElements()) { theEntry = (ZipEntry)enumer.nextElement(); totalLength += theEntry.getSize(); if (theEntry.getName().length() > maxFileNameLength) { maxFileNameLength = theEntry.getName().length(); } } System.out.println("Zip File " + lengthString(f.length()) + " and Total " + lengthString(totalLength) + " Data to unzip"); enumer = zf.entries(); while (enumer.hasMoreElements()) { theEntry = (ZipEntry)enumer.nextElement(); String entryName = theEntry.getName(); String entryName2 = ""; for (int i = 0; i < entryName.length(); i++) { char c = entryName.charAt(i); if (c == '\\') { c = '/'; } entryName2 += c; } entryName = entryName2; zipBase = "."; if (dest != null) { if (dest.endsWith("/")) { dest = dest.substring(0,dest.length() - 1); } zipBase = dest; } String tmpFolder = zipBase; int begin = 0; int end = 0; while (true) { end = entryName.indexOf("/",begin); if (end == -1) { break; } tmpFolder += "/" + entryName.substring(begin,end); begin = end + 1; f = new File(tmpFolder); if (!f.exists()) { f.mkdir(); } } OutputStream os = new FileOutputStream(zipBase + "/" + entryName); InputStream is = zf.getInputStream(theEntry); int bytesRead = is.read(buffer,0,bufsize); bytesHandled += bytesRead; while (bytesRead > 0) { os.write(buffer,0,bytesRead); bytesRead = is.read(buffer,0,bufsize); bytesHandled += bytesRead; System.out.print("\r"); int percent = (int)(((double)bytesHandled / totalLength) * 100 ); System.out.print("\r" + percent + "% "); if (totalLength > 1024 * 1024 * 80) { int j; for (j = 0; j < percent % 4 + 1; j++) { System.out.print(">"); } for (int x = 0; x < 4 - j; x++) { System.out.print(" "); } } System.out.print(" Unzip File " + entryName + getSpace(maxFileNameLength - entryName.length())); } is.close(); os.close(); } System.out.println("\r100% Zip File : " + zipFileName + " has been unCompressed to " + dest + "/ !" + getSpace(40)); } public static void main(String []args) throws Exception { if (args.length < 2) { System.out.println("usage : java cross.pauliuyou.tool.io.ZipUtil -zip[-unzip] filename"); return; } ZipUtil zip = new ZipUtil(); String dest = null; if (args[0].equals("-zip")) { if (args.length > 2) { dest = args[2]; } zip.zip(args[1],dest); } if (args[0].equals("-unzip")) { if (args.length > 2) { dest = args[2]; } zip.unzip(args[1],dest); } } }
试试其它关键字
压缩解压缩
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3