Preferred way to use Java ZipOutputStream and BufferedOutputStream

后端 未结 2 1668
终归单人心
终归单人心 2021-02-01 00:37

In Java does it matter whether I instantiate a ZipOutputStream first, or the BufferedOutputStream first? Example:

FileOutputStream dest = new FileOutputStream(fi         


        
2条回答
  •  春和景丽
    2021-02-01 01:26

    You should:

    ZipOutputStream out =  new ZipOutputStream(new BufferedOutputStream(dest));
    

    because you want to buffer the writing to the disc (because this is much more efficient in big data blocks than in a lot of little ones).


    This

    new BufferedOutputStream(new ZipOutputStream(dest));
    

    would buffer before zip compression. But this all happens in the memory and does not need buffering because a lot of little memory accesses are about the same speed as a few big ones. In memory general the needed time is proportional to the number of bytes read/write.

    As mentioned in the comments:

    The methods of ZipOutputStream which are not part of BufferedOutputStream would not be available also. E.g. putNextEntry and closeEntry.

提交回复
热议问题