遍历zip文件java中的所有目录 [英] iterating over all directories in a zip file java
问题描述
我目前正在开发一个工具,允许我修改zip文件的md5。该文件的目录结构如下所示:
baselines->
款 - >图标 - >
lang - >
(一堆文件)
然而,当我运行我的代码时,这些目录正在迭代进入。输出给我:
$ p $ 名称:model / visualization_dependency.xml
名称:model / visualization_template.xml
名称:model / weldmgmt_dependency.xml
名称:model / weldmgmt_template.xml
我是期望类似model / baseline / somefile.xml的东西会出现在输出中,但它不会。有什么想法吗?
byte [] digest = null;
MessageDigest md5;
尝试{
md5 = MessageDigest.getInstance(MD5);
ZipEntry current; ((current = entry.getNextEntry())!= null){
// ZipEntry current = entry.getNextEntry();
System.out.println(Size:+ current.getSize());
System.out.println(Name:+ current.getName()); (current.isDirectory()){
digest = this.encodeUTF8(current.getName());
if
md5.update(digest);
}
else {
int size =(int)current.getSize();
digest =新字节[size];
entry.read(digest,0,size);
md5.update(digest);
}
}
digest = md5.digest();
entry.close();
} catch(NoSuchAlgorithmException e){
// TODO自动生成的catch块
e.printStackTrace();
是目录,那么你需要迭代遍历目录中的每个文件并处理每个文件。
例子:
if(current.isDirectory()){
System.out.println(Directory:+ file.getName());
//通过file.listFiles()获取文件列表,并将它传递给
//传递给将执行处理的其他方法。
digest = this.encodeUTF8(current.getName());
md5.update(digest);
检查这个问题,它细节过程很好。
在Java中对目录进行迭代
I'm currently developing a tool that would allow me to modify the md5 of a zip file. The directory structure of the file looks like
baselines->
models -> icons ->
lang ->
(a bunch of files here)
However, when I run my code, none of those directories are getting iterating into. The output gives me:
Name:model/visualization_dependency.xml
Name:model/visualization_template.xml
Name:model/weldmgmt_dependency.xml
Name:model/weldmgmt_template.xml
I was expecting to something like model/baseline/somefile.xml appears on the output, but it does not. Any Thoughts?
byte[] digest = null;
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
ZipEntry current;
while((current = entry.getNextEntry()) != null){
//ZipEntry current = entry.getNextEntry();
System.out.println("Size:" + current.getSize());
System.out.println("Name:" + current.getName());
if(current.isDirectory()){
digest = this.encodeUTF8(current.getName());
md5.update(digest);
}
else{
int size = (int)current.getSize();
digest = new byte[size];
entry.read(digest, 0, size);
md5.update(digest);
}
}
digest = md5.digest();
entry.close();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Once you check that existing folder is directory then you need to iterative go through each files in the directory and process each on of those.
Example:
if(current.isDirectory()){
System.out.println("Directory: " + file.getName());
//Get list of files by file.listFiles() and pass it to
// to other method that will do processing.
digest = this.encodeUTF8(current.getName());
md5.update(digest);
}
Checkout this question, it details process well. Iterating inside directories in Java
这篇关于遍历zip文件java中的所有目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!