java zip - create text file of String and zip it

Create a text file with some string (without temporary file) and zip it : working source code java

 private static void zip_single() throws Exception {
  final StringBuilder sb = new StringBuilder();
  sb.append("Test String that goes into text file inside zip");

  final File f = new File("single_text_file_inside.zip");
  final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
  ZipEntry e = new ZipEntry("inside_text_file.txt");
  out.putNextEntry(e);

  byte[] data = sb.toString().getBytes();
  out.write(data, 0, data.length);
  out.closeEntry();

  out.close();
 }

Call as :
 public static void main(String[] args) throws Exception {
  zip_single();
 }

For creating text files from List of string and zip them - see my earlier post 

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...