Oxnard
Oxnard

Reputation: 269

writing a string to a zip file using ZipOutputStream

Ultimately the goal is just to zip a string and have that as a string in java

 public void zip() {
    try {
      String myTestString = "a zip file test and another test";
      InputStream in = new ByteArrayInputStream(myTestString.getBytes());
      OutputStream out = new ByteArrayOutputStream();
      ZipOutputStream zipOut = new ZipOutputStream(out);
      ZipEntry zipEntry = new ZipEntry("wtf.txt");
      zipOut.putNextEntry(zipEntry);
      zipOut.write(myTestString.getBytes(),0,myTestString.getBytes().length);
      zipOut.close();
      myTestString = out.toString();
      out.close();
      System.out.println(myTestString);
      
    
      // just a test if I can read the file 
      in = new ByteArrayInputStream(myTestString.getBytes());
      out = new FileOutputStream("c:\\t\\string.zip");
      byte[] allBytes = new byte[(int) myTestString.getBytes().length];
      in.read(allBytes);
      out.write(allBytes);
      out.close();
      
      
    } catch (IOException e) {
      e.printStackTrace();
    }
    
  }

I have found I can write a string to a zipfile using

  public void zipStringToFile2() {
    try {
      FileOutputStream fos = new FileOutputStream("c:/t/compressed.zip");
      ZipOutputStream zipOut = new ZipOutputStream(fos);
      String myTestString = "a zip file test and another test";
      int buffer = myTestString.getBytes().length;
//      byte[] myBytes = myTestString.getBytes();
      ByteArrayOutputStream out = new ByteArrayOutputStream(buffer);
      ZipEntry zipEntry = new ZipEntry("wtf.txt");
      zipOut.putNextEntry(zipEntry);
      zipOut.write(myTestString.getBytes(),0,buffer);
      zipOut.close();
      fos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

but I cannot get the output of zipOut to write to a string and then have the string write to a file and then via the OS open the zipfile. How can that be done?

Upvotes: 0

Views: 5005

Answers (2)

Oxnard
Oxnard

Reputation: 269

this is really what I was looking for:

  public void zip() {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ZipOutputStream zipOut = new ZipOutputStream(baos);
      
      ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
      System.out.println("testString " + testString + " testString.getBytes() " + testString.getBytes());

      ZipEntry zipEntry = new ZipEntry("Results.xml");
      zipOut.putNextEntry(zipEntry);
      
      byte[] bytes = new byte[1024];
      int length;
      while((length = bais.read(bytes)) >= 0 ) {
        zipOut.write(bytes, 0, length);
      }
      
      zipOut.close();
      bais.close();
      baos.close();
      
      byte[] zipBytes = baos.toByteArray();

      // just a test to see if can be opened in the operating system
      OutputStream os = new FileOutputStream(new File("c:/t/again.zip"));
      os.write(zipBytes);
      os.close();
    } catch(IOException e) {
      e.printStackTrace();
    }
  }

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 102903

myTestString = out.toString();

This doesn't do what you want. bytes aren't strings. toString() doesn't give useful information (it is a debugging tool). Keep the byte array (.toByteArray()).

out.close();

close the stream after you retrieved the data? Don't do that. Close first. (not that it matters, here. ByteArrayXStream's close() doesn't do anything at all. The point is, it either does nothing in which case you should remove it, or if it does have an effect, your code would be broken).

myTestString.getBytes()

No, don't ever call that method. It gives you the bytes by decoding the characters into bytes using 'platform default encoding'. Who knows what that is.

} catch (IOException e) {
    e.printStackTrace();
}

The correct 'I dunno' exception handler is throw new RuntimeException("unhandled", e);, not e.printStackTrace();. You get more info, and you get fewer WTFs (because yours will continue execution even though things are clearly wrong, which is a very bad idea).

but I cannot get the output of zipOut to write to a string

Yup. Strings aren't bytes, what you want is not possible at all. In any language. Some languages make it look like you can - those are bad languages, that conflate strings and byte arrays. Python decided to crash and burn and invent a whole new python (python2k -> python3k) to try to fix this, which goes to show. Boy that was a ton of pain and they suffered it to fix this oversight.

but I cannot get the output of zipOut to write to a string and then have the string write to a file and then via the OS open the zipfile. How can that be done?

So, replace all occurences of 'string' in that sentence with 'byte array' and all is peachy fine!

Upvotes: 2

Related Questions