In Java development, processing compressed files (such as ZIP, RAR, etc.) is a common task, especially when it is necessary to process large amounts of data, backup or distribute applications. The Java Standard Library (Java SE) provides native support for the ZIP format, throughClasses in the package to implement compression and decompression functions. This article will focus on how to use Java to decompress ZIP or RAR packages.
1. Decompression pack
To decompress the compressed package, with the help of the ZipInputStream class, you can read each file in the compressed package, and then write it to the corresponding path according to the read file attributes. For the structure of the decompressed compressed package, after reading a file, if it is a file under a multi-layer path, you need to create the parent directory first and then write to the file stream.
Decompress code implementation
// Unzip zip format public static void unzip(String path){ // Create source file (File object) based on the original path (string) File sourceFile = new File(path); // Root directory String sourceFileName = (); File rootDir = new File(()+"\\"+(0,("."))); // Determine whether the root directory already exists if(()){ // If it exists, delete // (); // Only empty directories can be deleted // Use the FileUtils tool class provided by the commons-io package to delete try { (rootDir); } catch (IOException e) { (); } } // Create root directory (); // (rootDir); // ZipInputStream: Used to perform compressed file input streams in zip format try (ZipInputStream in = new ZipInputStream(new FileInputStream(sourceFile))) { // traverse each subdirectory or subfile in the compressed package (object of type ZipEntry) ZipEntry zipEntry = null; while((zipEntry = ()) != null){ // (()); // Create a subdirectory or subfile (File object) // F:\Software\IDEA\Projects\test\easyftp-server-1.7.0.10-cn File file = new File(()+"\\"+()); if(()){ // Subdirectory creation of physical disk (); }else{ // Create subfiles in physical disk (); // Read the subfile in the current compressed package and write it to the new subfile through the output stream out try(FileOutputStream out = new FileOutputStream(file)) { byte[] buff = new byte[1024]; int len = -1; while((len = (buff)) != -1){ (buff,0,len); } } } } } catch (IOException e) { (); } }
Decompress code implementation
// Unzip rar format private static void unrar(String path) { // 1. Create the unzipped root directory File rarFile = new File(path); // File rootDir = new File(()+"\\"+().substring(0,().lastIndexOf("."))); String rarFileName = (); File rootDir = new File(()+"\\"+(0,("."))); if(()){ try { (rootDir); } catch (IOException e) { (); } } (); // Create Archive object to read rar compressed file format try(Archive archive = new Archive(new FileInputStream(path))) { // Read all subdirectories or subfiles in the compressed file (FileHeader object) List<FileHeader> fileHeaderList = (); // Sort by subdirectory (subfile) name (new Comparator<FileHeader>() { @Override public int compare(FileHeader o1, FileHeader o2) { return ().compareTo(()); } }); // traverse subdirectories and subfiles for (FileHeader fd:fileHeaderList) { (()); File f = new File(()+"\\"+()); if(()){ // Create a new subdirectory (); }else{ // Create a new subfile (); // Get the output stream of subfiles in the compressed package InputStream in = (fd); // Copy file input stream to new subfile (in,f); } } } catch (RarException e) { (); } catch (IOException e) { (); } }
3. Call the decompression method
Finally, inmain
Method or any other appropriate location callunzip
Method, the path to pass into the ZIP or RAR file and the target to be decompressed.
import ; import ; import ; import ; import .*; import ; import ; import ; import ; public static void main(String[] args) { // String path = "F:\\Software\\IDEA\\Projects\\test\\easyftp-server-1.7.0."; String path = "F:\\Software\\IDEA\\Projects\\test\\Experimental Case.rar"; if((".zip")) { unzip(path); } else if((".rar")){ unrar(path); } }
2. Things to note
- File path processing: When decompressing, pay attention to correctly handling file paths in ZIP files to avoid security risks (such as path traversal attacks).
- Exception handling: During the decompression process, you may encounter file read errors, write errors or permission problems, and these exceptions should be properly handled.
- Performance Optimization: For large ZIP files, consider using more efficient IO operations and flow control to optimize decompression speed.
- JAR packages used for compression: third-party libraries, such as commons-io packages, are required.
3. Summary
- During the decompression process, the main thing is to read the stream, pay attention to exception processing, and close the stream.
- When decompressing files, pay attention to the processing of empty folders.
This is the end of this article about the detailed explanation of the Java operation compression package decompression process. For more related Java compression package decompression content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!