0

I'm not looking for any answers that involve opening the zip file in a zip input or output stream. 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? 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. 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. 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:

nèñà?G¾Þ§V¨ö—‚?‰9³’?ÀM·p›a0„èwåÕüaEܵ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¨ö—‚‰9³’ÀM·p›a0„èwåÕüaEܵ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.

Imred_Gemu
  • 58
  • 9

3 Answers3

2

Why would you want to convert binary data to chars? I think it will be much better to InputStream/OutputStream using byte arrays. See http://www.javapractices.com/topic/TopicAction.do?Id=245 for examples.

mazaneicha
  • 8,794
  • 4
  • 33
  • 52
1

bw.write(ch) will write the entire array. Read will only fill in some of it, and return a number telling you how much. This is nothing to do with zip files, just with how IO works.

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. You are also converting bytes to characters and back again, which will corrupt the data in other ways. Stick to streams.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Thanks for adding some code to clarify what you meant, I misunderstood you at first. I used that method, but unfortunately it still comes out messed up. It comes out so that the zip/jar file itself isn't corrupted but all the entries in it are. – Imred_Gemu Sep 28 '13 at 15:36
0

see the ZipInputStream and ZipOutputStream classes

Edit: use plain FileInputStream and 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? Since you ant to copy the whole file, there is nothing special about it being a zip file

Community
  • 1
  • 1
vandale
  • 3,600
  • 3
  • 22
  • 39
  • No, he doesn't want to look inside the structure, just to copy the whole thing. – bmargulies Sep 28 '13 at 15:17
  • As the I stated twice in the question, I am try to do this without using zipinput/outputstream. – Imred_Gemu Sep 28 '13 at 15:19
  • you want to "read it's contents, and write them somewhere else" without using zips streams? If you want to copy the whole thing then don't use Readers/Writer use plain file input and output streams – vandale Sep 28 '13 at 15:20
  • @vandale correct, I am trying to take the compressed data and move it without decompressing it first, the problem is the bufferedreader/writer seems to randomly add ?'s in when doing this. – Imred_Gemu Sep 28 '13 at 15:24
  • @vandale to put it to an analogy, lets say I have a magic box that compresses anything that goes in it (representing the zip/jar file) and lots of smaller boxes that go inside it (representing the entries). – Imred_Gemu Sep 28 '13 at 15:26
  • @vandale instead of opening the magic box and then opening another magic box and moving the small boxes one at a time from the first box to the next, I want to just pick up the whole magic box and move it – Imred_Gemu Sep 28 '13 at 15:28
  • Thanks, your "see also" fixes my problem quite nicely. – Imred_Gemu Sep 28 '13 at 19:04