How to Compress or Extract Archives using Java
Overview
This article explains how to compress or extract archives. These archives could be Zip and Rar formats. It covers the following topics.
Format: ZIP
Format: ZIP - Password
Format: RAR
Other topics covered by this article.
Java Compress or Extract Archives
This page shows how to perform standard archive operations in Java using Aspose.ZIP for Java. Aspose.ZIP package can be added to you Java project using the following instructions Installing Aspose.ZIP for Java
How to ZIP files in Java
Steps: Create Zip file in Java
- Create an instance of Archive class.
- Add files using Archive.createEntry method.
- Zip the files using Archive.save method.
1try (Archive archive = new Archive()) {
2 archive.createEntry("entry_name1.dat", "input_file1.dat");
3 archive.createEntry("entry_name2.dat", "input_file2.dat");
4 archive.save("result_archive.zip");
5}
Archive class constructor creates a regular ZIP archive using Deflate compression algorithm and no encryption.
How to UnZIP files in Java
Steps: Unzip File to Folder in Java
- Create an instance of Archive class with your Zip file.
- UnZip the Zip file using Archive.extractToDirectory method to your folder.
1try (Archive archive = new Archive("input_archive.zip")) {
2 archive.extractToDirectory("\\outputDirectory");
3}
Archive class constructor can open any ZIP archive. ArchiveLoadOptions should be provided as a second parameter in case the input archive is password-protected (See example below).
How to ZIP files with password in Java
Steps: Create Zip file with Password in Java
- Create an instance of Archive class.
- Pass it ArchiveEntrySettings with the object of EncryptionSettings with the object of TraditionalEncryptionSettings having your password.
- Add as many files by calling Archive.createEntry method multiple times.
- Save it as Zip file using Archive.save method.
1try (Archive archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("pass")))) {
2 archive.createEntry("entry_name1.dat", "input_file1.dat");
3 archive.createEntry("entry_name2.dat", "input_file2.dat");
4 archive.save("result_archive.zip");
5}
The encryptionSettings parameter is used to create a password-protected ZIP archive.
How to UnZIP files with password in Java
Steps: Unzip Password Protected Zip file using Java
- Create an instance of Archive class.
- Pass it ArchiveLoadOptions object and set the password to decrypt entries using setDecryptionPassword method with your zip file password.
- UnZip the Zip file using Archive.extractToDirectory method to your folder.
1ArchiveLoadOptions options = new ArchiveLoadOptions();
2options.setDecryptionPassword("pass");
3try (Archive archive = new Archive("input_archive.zip", options)) {
4 archive.extractToDirectory("\\outputDirectory");
5}
The ArchiveLoadOptions with a decryption password set is used to open a password-protected ZIP archive.
How to extract RAR files in Java
Steps: Extract RAR file in Java
- Create an instance of RarArchive class with your RAR file.
- Extract the contents of RAR file by calling RarArchive.extractToDirectory method.
1try (RarArchive archive = new RarArchive("input_archive.rar")) {
2 archive.extractToDirectory("\\outputDirectory");
3}
In the above sample RarArchive class constructor opens non-protected RAR archive.
See Also
This article also covers these topics. The codes are same as above.
Format: ZIP
Compression
Compression Password Protected
Extraction Password Protected
Format: RAR