Suppose I have the following directory structure.
D:\reports\january\
Inside january there are suppose two excel files say A.xls and B.xls. There are many places where it has been written about how to zip files using java.util.zip
. But I want to zip the january folder itself inside reports folder so that both january and january.zip will be present inside reports. (That means when I unzip the january.zip file I should get the january folder).
Can anyone please provide me the code to do this using java.util.zip
. Please let me know whether this can be more easily done by using other libraries.
Thanks a lot...
Right-click on the folder/folders from which the files need to be copied and choose Copywhiz–>Copy from the menu as shown below: Go to the destination folder where you wish to create the . zip file and select Copywhiz–> Paste Advanced. Under the paste options select 'Paste as compressed .
Steps to Compress a File in JavaPut a ZipEntry object by calling the putNextEntry(ZipEntry) method on the ZipOutputStream. The ZipEntry class represents an entry of a compressed file in the ZIP file. Read all bytes from the original file by using the Files. readAllBytes(Path) method.
Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.
Have you tried Zeroturnaround Zip library? It's really neat! Zip a folder is just a one liner:
ZipUtil.pack(new File("D:\\reports\\january\\"), new File("D:\\reports\\january.zip"));
(thanks to Oleg Šelajev for the example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With