42

I'm trying to zip a file (for example foo.csv) and upload it to a server. I have a working version which creates a local copy and then deletes the local copy. How would I zip a file so I could send it without writing to the hard drive and do it purely in memory?

Sleep Deprived Bulbasaur
  • 2,368
  • 4
  • 21
  • 33

3 Answers3

96

Use ByteArrayOutputStream with ZipOutputStream to accomplish the task.

you can use ZipEntry to specify the files to be included into the zip file.

Here is an example of using the above classes,

String s = "hello world";

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ZipOutputStream zos = new ZipOutputStream(baos)) {

  /* File is not on the disk, test.txt indicates
     only the file name to be put into the zip */
  ZipEntry entry = new ZipEntry("test.txt"); 

  zos.putNextEntry(entry);
  zos.write(s.getBytes());
  zos.closeEntry();

  /* use more Entries to add more files
     and use closeEntry() to close each file entry */

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

now baos contains your zip file as a stream

gawi
  • 2,843
  • 4
  • 29
  • 44
Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
  • Make sure you call the close()-method in a finally block, or better: use the Automatic Resource Management of Java 7. – Puce May 12 '14 at 17:21
  • @Puce : I intended to show only a snippet but after your comment i felt that adding a more structured code would be good. Thanks for the comment.. :) – Thirumalai Parthasarathi May 12 '14 at 17:40
  • @BlackPanther could you elaborate more/ show a detailed version for if the origin file exists in a local directory. – Sleep Deprived Bulbasaur May 13 '14 at 14:22
  • @SleepDeprivedBulbasaur : [**This**](http://stackoverflow.com/questions/10103861/adding-files-to-zip-file/10103963#10103963) link can help you on that. – Thirumalai Parthasarathi May 14 '14 at 06:27
  • 8
    Check this post (http://stackoverflow.com/questions/25974354/creating-zip-file-in-memory-out-of-byte-zip-file-is-allways-corrupted). Remember to close the ZipOutputStream before invoking `getBytes` on ByteArrayOutputStream. – hencrice Aug 12 '15 at 22:35
  • @hencrice and posterity, be aware that the example in that link (https://stackoverflow.com/questions/25974354/creating-zip-file-in-memory-out-of-byte-zip-file-is-allways-corrupted) doesn't work in Java 7 or 8. They didn't add the ability to declare resource variables outside the try block until Java 9. The example in this post will work for Java 7/8 folks. – MikeyT May 14 '19 at 14:48
  • This is a very good example and I admire it. Also I can recommend just a simple addition - set appropriate encoding when use those example. try(ZipOutputStream zos = new ZipOutputStream(baos, StandardCharsets.UTF_8)) and zos.write(s.getBytes(StandardCharsets.UTF_8)); – Armer B. Jan 10 '22 at 09:38
4

As the NIO.2 API, which was introduce in Java SE 7, supports custom file systems you could try to combine an in-memory filesystem like https://github.com/marschall/memoryfilesystem and the Zip file system provided by Oracle.

Note: I've written some utility classes to work with the Zip file system.

The library is Open Source and it might help to get you started.

Here is the tutorial: http://softsmithy.sourceforge.net/lib/0.4/docs/tutorial/nio-file/index.html

You can download the library from here: http://sourceforge.net/projects/softsmithy/files/softsmithy/v0.4/

Or with Maven:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.4</version>   
</dependency>  
Puce
  • 37,247
  • 13
  • 80
  • 152
3

nifi MergeContent contain compressZip code

commons-io

public byte[] compressZip(ByteArrayOutputStream baos,String entryName) throws IOException {
    try (final ByteArrayOutputStream zipBaos = new ByteArrayOutputStream();
         final java.util.zip.ZipOutputStream out = new ZipOutputStream(zipBaos)) {
        final ZipEntry zipEntry = new ZipEntry(entryName);
        zipEntry.setSize(baos.size());
        out.putNextEntry(zipEntry);
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), out);
        out.closeEntry();
        out.finish();
        out.flush();
        return zipBaos.toByteArray();
    }
}
cclient
  • 847
  • 7
  • 5