Create a Zip File in Memory

前端 未结 3 733
悲哀的现实
悲哀的现实 2020-12-23 16: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

相关标签:
3条回答
  • 2020-12-23 17:21

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-23 17:22

    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

    0 讨论(0)
  • 2020-12-23 17:34

    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>  
    
    0 讨论(0)
提交回复
热议问题