The following tutorial, we show how to create zip archive from byte array.
1. Add dependency.
2. Implement The following example code.<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.15</version></dependency>
2.1 Write to file.
package com.haruka.compressfilebytearraydemo; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; public class App { public static void main( String[] args ) { ZipArchiveOutputStream zOutputStream = null; FileOutputStream fileOutputStream = null; try { List<byte[]> byteArrayListToWrite = new ArrayList<byte[]>(); byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 1".getBytes()); byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 2".getBytes()); byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 3".getBytes()); fileOutputStream = new FileOutputStream("C:\\DEMO\\bytearrayarchivedemo.zip"); //path to save archive file. zOutputStream = new ZipArchiveOutputStream(fileOutputStream); for(int i=0;i<byteArrayListToWrite.size();i++) { byte[] dataToWrite = byteArrayListToWrite.get(i); ZipArchiveEntry zEntry = new ZipArchiveEntry("file_"+i+".txt"); // create zip entry and specific file name. zEntry.setSize(dataToWrite.length); //set size of data. zOutputStream.putArchiveEntry(zEntry); //add entry. zOutputStream.write(dataToWrite); zOutputStream.closeArchiveEntry(); // commit added entry (commit file). } }catch(Exception ex) { ex.printStackTrace(); }finally { try { if(zOutputStream != null) { zOutputStream.flush(); //save file. zOutputStream.close(); } if(fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }2.2 Write to servlet response.
package com.haruka.compressfilebytearraydemoservlet.servlet; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; public class CompressFileByteArrayDemo extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ZipArchiveOutputStream zOutputStream = null; try { List<byte[]> byteArrayListToWrite = new ArrayList<byte[]>(); byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 1".getBytes()); byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 2".getBytes()); byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 3".getBytes()); response.setContentType("application/zip"); //set content type zOutputStream = new ZipArchiveOutputStream(response.getOutputStream()); for(int i=0;i<byteArrayListToWrite.size();i++) { byte[] dataToWrite = byteArrayListToWrite.get(i); ZipArchiveEntry zEntry = new ZipArchiveEntry("file_"+i+".txt"); // create zip entry and specific file name. zEntry.setSize(dataToWrite.length); //set size of data. zOutputStream.putArchiveEntry(zEntry); //add entry. zOutputStream.write(dataToWrite); zOutputStream.closeArchiveEntry(); // commit added entry (commit file). } }catch(Exception ex) { ex.printStackTrace(); }finally { try { if(zOutputStream != null) { zOutputStream.flush(); //save file. zOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }Furthermore, the example can be adapt with other outputStream for any purpose.
The example can be downloaded in following github:
1. For write to file example.
https://github.com/benzyaa/javademo/tree/master/compressfilebytearraydemo
2. For write to servlet response.
https://github.com/benzyaa/javademo/tree/master/compressfilebytearraydemoservlet
Further Information:
https://commons.apache.org/proper/commons-compress/index.html
https://mvnrepository.com/artifact/org.apache.commons/commons-compress
No comments:
Post a Comment