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

前文描述:

之前公司有一个 .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();






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值