Java 在android中以编程方式查找ZipFile中的条目数
我想在Zip文件程序中查找条目数。 操作系统是Android 我创建了一个ZipFile,如下所示:Java 在android中以编程方式查找ZipFile中的条目数,java,android,Java,Android,我想在Zip文件程序中查找条目数。 操作系统是Android 我创建了一个ZipFile,如下所示: ZipFile zf = new ZipFile(path); 但不知何故,我得到了以下例外: ZIpException:未找到EOCD:未压缩存档 请帮我解决那个问题 这是我解压文件的代码。如何显示解压缩操作的进度 FileInputStream fin = new FileInputStream(zipFile); ZipInputStream zin = new
ZipFile zf = new ZipFile(path);
但不知何故,我得到了以下例外:
ZIpException:未找到EOCD:未压缩存档
请帮我解决那个问题
这是我解压文件的代码。如何显示解压缩操作的进度
FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
int iIndex = 0;
while ((ze = zin.getNextEntry()) != null) {
iIndex++;
Log.d(TAG, "Unzipping " + iIndex + " " + ze.getName());
try {
if (ze.isDirectory()) {
dirChecker(ze.getName(), location + SLASH);
} else {
FileOutputStream fout = new FileOutputStream(
location + SLASH + ze.getName());
byte[] buffer = new byte[8192];
int len;
while ((len = zin.read(buffer)) != -1) {
fout.write(buffer, 0, len);
}
fout.close();
zin.closeEntry();
}
} catch (Exception e) {
e.printStackTrace();
}
}
zin.close();
Log.d(TAG, "unzipping completed.");
根据以下文件:
此运行时异常由ZipFile和ZipInputStream在
文件或流不是有效的zip文件
我的猜测是,要么你的zip文件损坏,要么你的路径不正确
如果你的zip文件损坏,你应该修复它
您可以通过调用
File File=新文件(路径)代码>
并且只有在file.exists()
基本上您尝试获取nextElement()然后从zip文件中读取它时才打开ZipFile。您可以为ZipEntry类型的枚举循环保留一个计数器,然后随着我们找到更多元素而递增
ZipFile zf = new ZipFile(f);
try {
for (Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements();) {
ZipEntry ze = e.nextElement();
String name = ze.getName();
if (name.endsWith(".txt")) {
InputStream in = zf.getInputStream(ze);
}
}
} finally {
zf.close();
}
ZipFile zf=新的ZipFile(f);
试一试{
对于(EnumerationZIpException:EOCD not found:not Zip archive,异常表示该文件不是Zip archive,但我可以使用ZipInputStream和ZipEntry类以编程方式将其解压缩。这是正确的解压缩。我想显示解压缩操作的进度。如何操作?请添加更多代码,不要问多个问题,以避免重复)ep it simpleI我只想在Android中找到Zip文件程序中的条目的总数这不是它的工作原理。你必须给出更多的代码并解释,到目前为止你得到了什么,你尝试了什么,如果你尝试调试它,你的基本概念等等。否则你可能会得到对你和你的同事都没有用的答案d浪费了时间……他刚写了,但我能用ZipInputStream和ZipEntry类以编程方式解压。解压正确。我想展示解压操作的进度。怎么做?@blackbelt,那又怎样?他必须提出一个新问题。//引用:“请告诉我为什么会出现这样的异常。”@FelixLahmer:同意。这改变了太多问题。抱歉,伙计们,我指的是,但我能够使用ZipInputStream和ZipEntry以编程方式解压,所以问题可能与错误路径无关。我的错误FileInputStream fin=新FileInputStream(zipFile);ZipInputStream zin=新ZipInputStream(fin);ZipEntry ze=null;int-iIndex=0;while((ze=zin.getnextery())!=null){iIndex++;尝试{if(ze.isDirectory()){dirChecker(ze.getName(),location+SLASH);}else{FileOutputStream fout=newfileoutputstream(location+SLASH+ze.getName());byte[]buffer=new byte[8192];int len((len=zin.read(buffer)(buffer))=-1){fout.write(buffer,0,len);}fout.close();zin.closeEntry();}}}catch(异常e){e.printStackTrace();}}}这是我要解压缩的代码。