For Zipping a file in Java, we have to
- Get the File Stream or Byte Stream
- Initialize a ZipOutputStream.
- Create a ZipEntry for each files or byte arrays and add it in ZipOutputStream and close the Zip Entry.
- Once completed, update the ZipOutputStream by calling finish().
- Save it using FileOutputStream or Get the bytearray using ByteArrayOutputStream
[Zipping files in the form of byte array]
[Dynamically add files in the form of byte array to zip file in the form of byte array]
1 |
Program to read Files and compress to a zip File |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ReadFilesAndZip { public static void main(String[] args) { File files[] = new File[3]; files[0] = new File("C:/dummy/SampleFile.txt"); files[1] = new File("C:/dummy/ContentFile.txt"); files[2] = new File("C:/dummy/ContentFile2.txt"); //byte[]contentInBytes = readFile(files[0]); //System.out.println(new String(contentInBytes)); byte[] zipFileInBytes = zipMultipleFiles(files); writeFileInBytes(zipFileInBytes); } /* Read the file content and return in byte array */ public static byte[] readFile(File file) { byte[] fileContent = null; BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); int fileSize = bis.available(); fileContent = new byte[fileSize]; bis.read(fileContent); if(bis!=null) { bis.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fileContent; } /* This function wii write the input byte array content in the file location.*/ public static void writeFileInBytes(byte[] ContentInBytes) { try { FileOutputStream fout = new FileOutputStream("C:/dummy/CompressedFile.zip"); fout.write(ContentInBytes); if(fout!=null) { fout.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* Zip Multiple Files and return the Zip File in Byte Array., */ public static byte[] zipMultipleFiles (File[] files) { byte[] filesInZip = null; ByteArrayOutputStream bOutStream = new ByteArrayOutputStream(); ZipOutputStream zOutStream = new ZipOutputStream(bOutStream); for(int i=0;i<files.length;i++) { File tempFile = files[i]; byte[] fileContentInBytes = readFile(tempFile); if(fileContentInBytes!=null && fileContentInBytes.length>0) { String eachFileName = tempFile.getName(); ZipEntry ze = new ZipEntry(eachFileName); try { zOutStream.putNextEntry(ze); zOutStream.write(fileContentInBytes,0, fileContentInBytes.length); zOutStream.closeEntry(); } catch (IOException e) { e.printStackTrace(); } } } try { zOutStream.finish(); } catch (IOException e1) { e1.printStackTrace(); } filesInZip = bOutStream.toByteArray(); try { zOutStream.close(); } catch (IOException e) { e.printStackTrace(); } return filesInZip; } } |