【发布时间】:2014-07-16 09:44:48
【问题描述】:
我必须解压缩一个通过 URL 寻址的 zip 数据包。 (URL和DataInputStream都是正确的!)
private void unZipIt(File baseDir, DataInputStream dis, PipedOutputStream pos) throws ZipException
{
byte[] buffer = new byte[1024];
String fileName = "";
File newFile;
FileOutputStream fos = null;
try
{
ZipInputStream zis = new ZipInputStream(dis);
ZipEntry ze = zis.getNextEntry();
if(ze==null)
{
System.out.println("first zip entry is null");
}
while (ze != null)
{
System.out.println("zip entry is not null");
fileName = ze.getName();
newFile = new File(baseDir + File.separator + fileName);
if (isSavFile(fileName))
{
System.out.println(fileName + "is savfile");
new File(newFile.getParent()).mkdirs();
fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
// fos.close();
}
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
// return true;
}
catch (IOException e)
{
// ex.printStackTrace();
log.error("unzipping '" + fileName + "'", e);
throw new ZipException(fileName, e);
// return false;
}
但无法获取任何 zip 条目。 (第一个 zip 条目为空!) 使用 FileOutputStream 可以完美运行。由于效率原因,我不允许将文件存储在 PC 上。
谁有想法?
【问题讨论】:
-
更新。我错过了复制最后一个 { 以关闭该方法 - 但它肯定存在 ;)
-
你能显示调用的代码块“unZipIt”以及如何初始化DataInputStream吗?
-
当您调用此方法时,您可能已经位于流的末尾。
-
而且你需要关闭 FileOutputStream,否则你会泄漏文件描述符。
-
DataInputStream 是 url.openStream() - URL 是正确的!调用 UnzipIt(...) 的函数是: private boolean unzipAllFiles() throws ZipException { File savDir = vManager.getSaveDir(); System.out.println("saveDir:" + savDir.getPath()); if (!savDir.exists()) { savDir.mkdir(); } 尝试 { unZipIt(savDir, dis, pos); } catch (ZipException e) { throw e; } 返回真; }
标签: java zip fileinputstream datainputstream