java压缩⽂件⼤⼩不变_Java对zip中⽂件基于⼤⼩的对⽐处理zip⽂件可以通过ZipFile类得到初始化时传⼊⽂件名filename new ZipFile(filename );
获得zip⽂件后可以根据每个⼦⽂件⼤⼩和⼦⽂件的名字构建map,传⼊ies()到下⾯⽅法可构建
private static Map getZipEntriesSizeMap(
Enumeration extends ZipEntry> entries) {
Map fileMap = new HashMap();
while (entries.hasMoreElements()) {
ZipEntry first = Element();
fileMap.Name(), Size());
}
使用dom4j解析xml文件return fileMap;
}
获得map后可以根据两个map中的key进⾏对⽐value值
Iterator ite = firstMap.keySet().iterator();
while (ite.hasNext()) {
String fileName = ();
if (ainsKey(fileName)) {
if ((fileName).(fileName))) {
// when the file in first zip and second zip is same i
// remove it
} else {
/
/添加到⼀个定义的输出列中
}
}
对⽐时应对⽐两次 即第⼀个zip包⽣成的map和第⼆个⽐对后,应把第⼆个和第⼀个再次⽐对,因为双⽅都有可能有对⽅所没有的⽂件
⽽对于⼦⽂件是jar⽂件或者zip⽂件时也可以进⾏⽐较代码如下
if (Name().lastIndexOf(".jar") != -1 || Name().lastIndexOf(".zip") != -1) {
InputStream in = InputStream(file);
temp = ateTempFile("leotemp", ".tmp");
temp.deleteOnExit();
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(temp));
byte[] buffer = new byte[2048];
int nBytes = 0;
while ((nBytes = in.read(buffer)) > 0) {
out.write(buffer, 0, nBytes);
}
out.close();
ZipFile subzip = new ZipFile(temp);
if(parentPath != null && im() != ""){
fileMap.putAll(getZipEntriesSizeMap(subzip ,parentPath+" : "+Name()));
}else{
fileMap.putAll(getZipEntriesSizeMap(subzip ,Name()));
}
temp.delete();
}
parentPath为⽗⽂件的路径名字,第⼀传⼊时可以为空或者""
⼀般我会输出为⼀个xml⽂件,xml⽂件利⽤dom4j的jar包很容易创建并实现值得注意的只有⼀点,⼯具制作成jar包运⾏时,内部的路径应为相对路径即⾸字符应为"/包名/⽂件名"形式才能被访问到
分享到:
2012-05-17 14:51
浏览 1119
评论
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论