0

Actually I was trying to zip files using zipOutputStream but everytime it is not able to zip files properly. It gets stuck at a particular file of bigger size. My code looks like below:

           byte[] buf = new byte[1600*1024]; 
           long totalBytes = 0;
            if(flag){
                FileInputStream fileStream = new FileInputStream(absoluteFileName);
                zip.putNextEntry(new ZipEntry(fileName));
                while ((len = fileStream.read(buf)) > 0) {
                    System.out.println("bytes read = "+len);
                    totalBytes += len;
                    System.out.println("totalBytes = "+totalBytes);
                    zip.write(buf, 0, len);
                }
            }

The output looks like below:

  bytes read = 163840
  Total bytes = 163840
  bytes read = 163840
  Total bytes = 327680
  bytes read = 7940
  Total Bytes = 335620

Somehow it is not able to read beyond that. The file size is 336 KB. So, I believe the total bytes read is file size. But when trying to open the zip file , I get some error and the file extracted is not complete. The one extracted from zip is only 2468 lines while the original file is more than 3500 lines.

Please help. I am stuck in this for last 4 days.

Sarwan
  • 595
  • 3
  • 8
  • 21
  • 2
    Did you close output file properly? – talex Dec 27 '18 at 10:34
  • output file means zip file or the one which we are reading? – Sarwan Dec 27 '18 at 10:38
  • zip.close(); fileStream.close(); hope this guide can help you: https://www.baeldung.com/java-compress-and-uncompress – Infern0 Dec 27 '18 at 10:40
  • You need to close the entry (https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipOutputStream.html#closeEntry--) and close the output stream (https://docs.oracle.com/javase/8/docs/api/index.html?java/util/zip/ZipOutputStream.html) – JB Nizet Dec 27 '18 at 10:44
  • No. It did not work. – Sarwan Dec 27 '18 at 10:49
  • What do you mean by `Somehow it is not able to read beyond that.` ? Just afterwards you say you believe the whole file has been read; do you expect any other read? Apart from the corrupt zip and its incomplete entry, do you have any other error or weird behaviour? – Aaron Dec 27 '18 at 10:51
  • Yes, when I try to extract using the command tar -xvf file.zip , it gives me error as: trucated zip file body – Sarwan Dec 27 '18 at 10:59
  • @Sarwan do not use tar but unzip to unzip. – Thorbjørn Ravn Andersen Dec 27 '18 at 12:01

0 Answers0