简体   繁体   English

读取zip或jar文件,而无需先解压缩

[英]Read zip or jar file without unzipping it first

I'm not looking for any answers that involve opening the zip file in a zip input or output stream. 我没有寻找任何涉及在zip输入或输出流中打开zip文件的答案。 My question is is it possible in java to just simply open a jar file like any other file (using buffered reader/writer), read it's contents, and write them somewhere else? 我的问题是,是否有可能在Java中像其他文件一样简单地打开一个jar文件(使用缓冲的读取器/写入器),读取其内容,然后将其写入其他地方? For example: 例如:

import java.io.*;

public class zipReader {

public static void main(String[] args){

    BufferedReader br = new BufferedReader(new FileReader((System.getProperty("user.home").replaceAll("\\\\", "/") + "/Desktop/foo.zip")));
    BufferedWriter bw = new BufferedWriter(new FileWriter((System.getProperty("user.home").replaceAll("\\\\", "/") + "/Desktop/baf.zip")));
    char[] ch = new char[180000];

    while(br.read(ch) > 0){

        bw.write(ch);
        bw.flush();

    }

    br.close();
    bw.close();

}

}

This works on some small zip/jar files, but most of the time will just corrupt them making it impossible to unzip or execute them. 这适用于一些小的zip / jar文件,但大多数情况下只会破坏它们,因此无法解压缩或执行它们。 I have found that setting the size of the char[] to 1 will not corrupt the file, just everything in it, meaning I can open the file in an archive program but all it's entries will be corrupted and unusable. 我发现将char []的大小设置为1不会破坏文件,只是破坏其中的所有内容,这意味着我可以在存档程序中打开文件,但是其所有条目都会被破坏且无法使用。 Does anyone know how to write the above code so it won't corrupt the file? 有谁知道如何编写上面的代码,以免损坏文件? Also here is a line from a jar file I tested this on that became corrupted: 这也是来自jar文件的一行,我对该文件进行了测试,发现该文件已损坏:

nèñà?G¾Þ§ V? nèñàG¾Þ§V V? ¨ö—‚?‰9³'?ÀM·p›a0?„èwåÕüa?Eܵp‡aæOùR‰(JºJ´êgžè*?”6ftöãÝÈ—ê@qïc3âi,áž…¹¿Êð)V¢cã>Ê”G˜(†®9öCçM?€ÔÙÆC†ÑÝ×ok?ý—¥úûFs.‡ ¨ö‰?9³'?M·p› a0?„èwåÕüa?Eܵp‡aæOùR‰(JºJ´êgžè *?” 6ftöãÝÈ—ê @qïc3âi,áž…¹¿Êð)V¢cã>Ê” G〜( †®9öCçM?€ÔÙÆC†ÑÝ×ok?ý—¥úûFs。‡

vs the original: 与原始版本:

nèñàG¾Þ§ V? nèñàG¾Þ§§V V? ¨ö—‚‰9³'ÀM·p›a0?„èwåÕüa?Eܵp‡aæOùR‰(JºJ´êgžè*?”6ftöãÝÈ—ê@qïc3âi,áž…¹¿Êð)V¢cã>Ê”G˜(†®9öCçM€ÔÙÆC†ÑÝ×oký—¥úûFs.‡ ö9³'ÀM·p› a0?„èwåÕüa?Eܵp‡aæOùR‰(JºJ´êgžè *?”6ftöãÝÈ-ê@qïc3âi,áž…¹¿Êð)V¢cã>Ê” G∼(†® 9öCçM€ÔÙÆC†ÑÝ×oký—¥úûFs。‡

As you can see either the reader or writer adds ?'s into the files and I can't figure out why. 如您所见,读取器或写入器都会在文件中添加?,而我不知道为什么。 Again I don't want any answers telling me to open it entry by entry, I already know how to do that, if anyone knows the answer to my question please share it. 同样,我也不想让任何答案告诉我逐项打开它,我已经知道该怎么做,如果有人知道我的问题的答案,请分享。

Why would you want to convert binary data to chars? 为什么要将二进制数据转换为char? I think it will be much better to InputStream/OutputStream using byte arrays. 我认为使用字节数组对InputStream / OutputStream会更好。 See http://www.javapractices.com/topic/TopicAction.do?Id=245 for examples. 有关示例,请参见http://www.javapractices.com/topic/TopicAction.do?Id=245

bw.write(ch) will write the entire array. bw.write(ch)将写入整个数组。 Read will only fill in some of it, and return a number telling you how much. Read只会填充其中的一部分,并返回一个数字告诉您多少。 This is nothing to do with zip files, just with how IO works. 这与zip文件无关,而与IO的工作方式无关。

You need to change your code to look more like: 您需要更改代码,使其看起来更像:

 int charsRead = br.read(buffer);
 if (charsRead >= 0) {
       bw.write(buffer, 0, charsRead);
 } else {
       // whatever I do at the end.
 }

However, this is only 1/2 of your problem. 但是,这只是您问题的1/2。 You are also converting bytes to characters and back again, which will corrupt the data in other ways. 您还将字节转换为字符,然后又转换回来,这将以其他方式破坏数据。 Stick to streams. 坚持流。

see the ZipInputStream and ZipOutputStream classes 参见ZipInputStreamZipOutputStream

Edit: use plain FileInputStream and FileOutputStream. 编辑:使用普通的FileInputStream和FileOutputStream。 I suspect there may be some issues when the reader is interpreting the bytes as characters. 我怀疑读者将字节解释为字符时可能会出现一些问题。

see also: Standard concise way to copy a file in Java? 另请参见: 在Java中复制文件的标准简洁方法? Since you ant to copy the whole file, there is nothing special about it being a zip file 由于您可以复制整个文件,因此作为zip文件没有什么特别的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM