Hi,
I have following method that adds passed arrays of txt files to zip. The method was working fine until I added a condition if (in.read() != -1){} that checks if a file is empty and skip it.
That works but, in every file added to a zip now first two characters on the first line are truncated like that
0000234...
P00000434...
P00000254...
instead of
P00000234...
P00000434...
P00000254...
Here is the method:
public void compressFiles(String[] fileArray, String zipFileName){ byte[] buf = new byte[1024]; try { // Create the ZIP file String outFilename = zipFileName; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); // Compress the files for (int i=0; i < fileArray.length; i++) { FileInputStream in = new FileInputStream(fileArray[i]); if (in.read() != -1){ // check if a file is not empty // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(fileArray[i])); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); } in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { } }
Any help is greatly appreciated.
Thanks,
Gena