java - create text files from list of string and zip them ( without creating temporary file)
private static void list_of_string_into_text_file_and_zip_them_all(List dataList) throws Exception {
File zipFile = new File("OutputZipFile.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
int count = 0;
for (String string : dataList) {
// repeat for each string
String fileNameInsideZip = ++count + "-inside_text_file.txt";
ZipEntry e = new ZipEntry(fileNameInsideZip);
out.putNextEntry(e);
// write data into zip entry
byte[] data = string.getBytes();
out.write(data, 0, data.length);
out.closeEntry();
}
out.flush();
out.close();
}
Testing the code :
public static void main(String[] args) throws Exception {
// store string into text file - and zip them . doesnot create a
// temporary text file.
List test = new ArrayList();
test.add("This is amazing");
test.add("What a wonderful world");
test.add("This is a beautiful day ");
list_of_string_into_text_file_and_zip_them_all(test);
}
For creating a single text file from string and zip it -
see this post.