您的位置:首页 > 编程语言 > Java开发

【记录】使用 ZipInputStream类getNextEntry方法遇到的错误

2016-04-18 14:41 513 查看
前文描述:

之前公司有一个 .exe 的工具:A:可以把一个文件放进压缩包,B:可以检验被放进压缩包的文件。

现在需要用Java编写一个类似的工具。

1、工具写好之后进行验证时发现:

1)用java代码工具进行AB操作时完全正常。

2)用java代码工具进行A操作用exe工具进行B操作正常。

3)用exe工具进行A操作,用java工具进行B操作抛出异常。异常如下:

java.util.zip.ZipException: invalid entry size (expected 67324752 but got 5896 bytes)
at java.util.zip.ZipInputStream.readEnd(Unknown Source)
at java.util.zip.ZipInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.closeEntry(Unknown Source)
at java.util.zip.ZipInputStream.getNextEntry(Unknown Source)
at com.iapppay.apk.mark.tool.util.ToolMain.readMark(ToolMain.java:127)
at com.iapppay.apk.mark.tool.ui.ReadMarkPanel.getMarkFromApk(ReadMarkPanel.java:86)
at com.iapppay.apk.mark.tool.ui.ReadMarkPanel.actionPerformed(ReadMarkPanel.java:72)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2、问题详细信息:
exe进行A操作的代码:

//拷贝zip文件
string tmpZipPath = zipFolder + "\\" + mark + "_" + zipFileName;
System.IO.File.Copy(zipPath, tmpApkPath, true);

//将mark对应到文件添加到apk
using (ICSharpCode.SharpZipLib.Zip.ZipFile zip = new ICSharpCode.SharpZipLib.Zip.ZipFile(tmpZipPath))
{
zip.BeginUpdate();
zip.Add(markPath, "META-INF\\" + markEnc);
zip.CommitUpdate();
zip.Close();
}

java工具进行B操作的代码:
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null){
String entryName = entry.getName();
if(entryName.contains(ChannelUtil.CHANNEL_DIR+ChannelUtil.MARK_FILE_MARKS)){
break;
}
}
zis.closeEntry();
zis.close();

3.解决办法:
java工具进行B操作的代码修改为:

ZipFile zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> enumeration= zipFile.entries();

while(enumeration.hasMoreElements()){
String entryName = enumeration.nextElement().getName();
if(entryName.startsWith(ChannelUtil.CHANNEL_DIR+ChannelUtil.MARK_FILE_MARKS)){
break;
}
}
zipFile.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息