Java unzip file with directories
Ask Question. Asked 9 years, 8 months ago. Active 10 months ago. Viewed 54k times. Michael Berry Waypoint Waypoint Add a comment. Active Oldest Votes. I am not sure what do you mean by particaly? Do you mean do it yourself without of API help? In the case you don't mind using some opensource library, there is a cool API for that out there called zip4J It is easy to use and I think there is good feedback about it. Valentin Michalak 1, 1 1 gold badge 13 13 silver badges 24 24 bronze badges.
Here is the code I'm using. BufferedOutputStream; import java. File; import java. FileOutputStream; import java. IOException; import java. ZipEntry; import java. You should not swallow IOException. Oliv Oliv 9, 3 3 gold badges 46 46 silver badges 71 71 bronze badges.
Same can be achieved using Ant Compress library. It will preserve the folder structure. Kumar Sambhav Kumar Sambhav 6, 13 13 gold badges 57 57 silver badges 82 82 bronze badges. Hello, the class org. Unzip don't have the method setSrc , setDest or execute. Just learned the hard way that ZipEntry getSize javadoc ends with an ominous " I made a small edit to avoid the long parade of NegativeArraySizeExceptions. For example take directory named as first which is located in E disk.
It contains one subfolder along with multiple files. We have to zip it now. Step 1: We should list out all files in the directory. We can use this method to list out the total files. For this we have to check whether it is a directory or not.
If it is a directory then again list out the files in that subdirectory. Continue this process until all files are listed out. This is recursive process. Step 2: After listing out the files, we should create a file output stream for output zip folder.
So that it will create output zip folder and write the contents to it. Step 3: We should create a zip output stream which will write file contents to the file output stream in the format of zip. Step 5 : As zipping of all files is done. Now close all IO streams.
Proper closing of IO streams will prevent the corruption of zip files. Unzipping file means expanding the compressed information to make file as like its original form means before compression. For that ZipInputStream class will be useful. Step 1: We should create file output stream for output folder. So that it can write files to that output folder.
If output folder is not exists then create output folder. Use mkdirs method. If any parent directories are not there then it will create them also. Step 2: Here we have only one file that is zip file. So create one file input stream this is enough. This will read contents from zip file.
Step 3: Now create Zip input stream for this file input stream. This zip input stream can understand the zip file contents. It will change zip form to normal form.
For example, extracting zip file tomcat. My unzip command implementation uses the builder pattern so that you can pass arguments as named parameters before launching the unzip command.
There are currently 3 parameters:. Here is my implementation see it on GitHub :. In my implementation, the file input stream and ZIP input stream are used to read and extract entries.
They are automatically and safely closed at the end using the try-with-resources statement. ZipEntry and is visited using ZIP input stream. The entry list will be exhausted when all entries are visited once. In other words, the list will be exhauste when the next entry will be null. Note that ZIP entry can be either a directory or a regular file, they need to be treated differently.
The size of the output buffer byte array is controlled by the parameter bufferSize.
0コメント