如何使用 Java 读取 Winzip 自解压 (exe) zip 文件?
是否有现有的方法或者我需要在将数据传递到 ZipInputStream 之前手动解析并跳过 exe 块?
Is there an existing method or will I need to manually parse and skip the exe block before passing the data to ZipInputStream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TrueZip 在这种情况下效果最好。 (至少在我的情况下)
自解压 zip 的格式如下:code1 header1 file1(而普通 zip 的格式为 header1 file1)...代码说明如何解压 zip
尽管 Truezip 解压实用程序抱怨额外的字节并引发异常
这是代码
您可以像 Extract(new String("C:\2006Production.exe"), new String("c:\") , ""); 一样调用此方法;
文件被解压到c盘...您可以对您的文件执行您自己的操作。我希望这有帮助。
谢谢。
TrueZip works best in this case. (Atleast in my case)
The self extracting zip is of the following format code1 header1 file1 (while a normal zip is of the format header1 file1)...The code tells on how to extract the zip
Though the Truezip extracting utility complains about the extra bytes and throws an exception
Here is the code
You can call this method like Extract(new String("C:\2006Production.exe"), new String("c:\") , "");
The file is extracted in the c drive...you can perform your own operation on your file. I hope this helps.
Thanks.
查看EXE 文件格式和ZIP 文件格式 并测试各种选项,看来最简单的解决方案是忽略任何前导码第一个 zip 本地文件头。
我编写了一个输入流过滤器来绕过前导码,它工作得很好:
WinZipInputStream.java
After reviewing the EXE file format and the ZIP file format and testing various options it appears the easiest solution is to just ignore any preamble up to the first zip local file header.
I wrote an input stream filter to bypass the preamble and it works perfectly:
WinZipInputStream.java
ZIP 文件的优点在于它们的顺序结构:每个条目都是一组独立的字节,最后是一个中央目录索引,列出了所有条目及其在文件中的偏移量。
糟糕的是,
java.util.zip.*
类忽略该索引,只是开始读入文件并期望第一个条目是本地文件头 块,但自解压 ZIP 存档的情况并非如此(这些存档以 EXE 部分开头)。几年前,我编写了一个自定义 ZIP 解析器来提取单个 ZIP 条目(LFH + 数据),该解析器依赖 CDI 来查找这些条目在文件中的位置。我刚刚检查过,它实际上可以毫不费力地列出自解压 ZIP 存档的条目,并为您提供偏移量 - 因此您可以:
使用该代码查找 EXE 部分之后的第一个 LFH ,并将该偏移量之后的所有内容复制到另一个File
中,然后将该新的File
提供给java.util.zip.ZipFile< /代码>
:
编辑:仅仅跳过 EXE 部分似乎不起作用,
ZipFile
仍然无法读取它,我的本机 ZIP 程序抱怨新的 ZIP 文件是损坏并且我跳过的确切字节数被指定为“丢失”(因此它实际上读取了 CDI)。我想一些标头需要重写,因此下面给出的第二种方法看起来更有希望 - 或者代码托管在 SourceForge (项目页面,网站)并根据 Apache License 2.0 获得许可,因此商业用途很好 - AFAIK 有一个商业游戏使用它作为其游戏资产的更新程序。
从 ZIP 文件获取偏移量的有趣部分位于
Indexer.parseZipFile
返回一个LinkedHashMap
(因此第一个映射条目在文件中具有最低的偏移量)。下面是我用来列出自解压 ZIP 存档条目的代码(使用 WinZIP SE 创建器和 Ubuntu 上的 Wine 从 acra 发布文件):除了包含所有标头的
Indexer
类和zip
包之外,您可能可以删除大部分代码解析类。The nice thing about ZIP files is their sequential structure: Every entry is a independent bunch of bytes, and at the end is a Central Directory Index that lists all entries and their offsets in the file.
The bad thing is, the
java.util.zip.*
classes ignore that index and just start reading into the file and expect the first entry to be a Local File Header block, which isn't the case for self-extracting ZIP archives (these start with the EXE part).Some years ago, I wrote a custom ZIP parser to extract individual ZIP entries (LFH + data) that relied on the CDI to find where these entries where in the file. I just checked and it can actually list the entries of a self-extracing ZIP archive without further ado and give you the offsets -- so you could either:
use that code to find the first LFH after the EXE part, and copy everything after that offset to a different:File
, then feed that newFile
tojava.util.zip.ZipFile
Edit: Just skipping the EXE part doesn't seem to work,
ZipFile
still won't read it and my native ZIP program complains that the new ZIP file is damaged and exactly the number of bytes I skipped are given as "missing" (so it actually reads the CDI). I guess some headers would need to be rewritten, so the second approach given below looks more promising -- orjava.util.zip
); this would require some additional plumbing because the code originally wasn't intended as replacement ZIP library but had a very specific use case (differential updating of ZIP files over HTTP)The code is hosted at SourceForge (project page, website) and licensed under Apache License 2.0, so commercial use is fine -- AFAIK there's a commercial game using it as updater for their game assets.
The interesting parts to get the offsets from a ZIP file are in
Indexer.parseZipFile
which returns aLinkedHashMap<Resource, Long>
(so the first map entry has the lowest offset in the file). Here's the code I used to list the entries of a self-extracting ZIP archive (created with the WinZIP SE creator with Wine on Ubuntu from an acra release file):You can probably rip out most of the code except for the
Indexer
class andzip
package that contains all the header parsing classes.某些自解压 ZIP 文件中存在虚假的本地文件头标记。我认为最好向后扫描文件以查找中央目录结尾记录。 EOCD记录包含中央目录的偏移量,CD包含第一个本地文件头的偏移量。如果您从本地文件头的第一个字节开始读取,
ZipInputStream
工作正常。显然下面的代码不是最快的解决方案。如果要处理大文件,您应该实现某种缓冲或使用内存映射文件。
There are fake Local File Header markers in some self-extracting ZIP files. I think it's best to scan a file backwards to find End Of Central Directory record. EOCD record contains offset of a Central Directory, and CD contains offset of the first Local File Header. If you start reading from the first byte of a Local File Header
ZipInputStream
works fine.Obviously the code below is not the fastest solution. If you are going to process large files you should implement some kind of buffering or use memory mapped files.