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?
问题:
回答1:
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
回答2:
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>