I want to compress a file into zip, rar and 7z format using java code. Also I want to decompress these files at a specified location. Can anyone please tell me how to compress and decompress files using 7-zip in java?
-
4Did you try punching `7zip java` into a search engine? You would have found hits like [7-Zip-JBinding](http://sevenzipjbind.sourceforge.net/). – David Schwartz Oct 08 '12 at 11:55
-
I have tried it but I could only get the list of files. – user1728310 Oct 08 '12 at 12:01
-
Go to [this page](http://sourceforge.net/projects/sevenzipjbind/files/7-Zip-JBinding/4.65-1.05rc-extr-only/). There's a link right at the top if you want the Windows version. – David Schwartz Oct 08 '12 at 12:06
-
Thanks David I used these jars: sevenzipjbinding.jar sevenzipjbinding-Allplatforms.jar But I think they can be used only for the extraction of compressed file. Is there any way to compress files using 7-zip? – user1728310 Oct 09 '12 at 10:26
-
@user1728310 Since you have tried so many things, would you mind telling us what you have tried? – Code-Apprentice Oct 10 '12 at 04:08
-
As i told earlier, I have used sevenzipjbinding.jar sevenzipjbinding-Allplatforms.jar and I am able to decompress files using these jars. – user1728310 Oct 10 '12 at 04:53
-
You can get the code for de-compression from following link : http://sourceforge.net/projects/sevenzipjbind/forums/forum/757964/topic/3844899 – user1728310 Oct 10 '12 at 04:57
-
1@DavidSchwartz a binding means native code. That may not be an option in a pure Java environment. – Thorbjørn Ravn Andersen Apr 15 '13 at 11:46
-
Possible duplicate of [How to use LZMA SDK to compress/decompress in Java](https://stackoverflow.com/questions/5481487/how-to-use-lzma-sdk-to-compress-decompress-in-java) – Vadzim Feb 16 '18 at 20:11
3 Answers
I have used : sevenzipjbinding.jar sevenzipjbinding-Allplatforms.jar
I am now able to decompress files using these jars.
Try this link for decompression: http://sourceforge.net/projects/sevenzipjbind/forums/forum/757964/topic/3844899

- 687
- 2
- 5
- 14
SevenZipBinding is great for decompression, it even detects format automaticaly. Problem is it can't compress. You can create zip archives using ZIP4J. It however offers only plain zip. LZMA apparently can compress single files into 7z archives, but I haven't tried it yet.

- 7,590
- 5
- 49
- 75

- 109
- 7
You can also write a batch script or just inline command execution which you call from java to extract and compress. Note that this option is for windows platforms only and required a few admin skills. 7zip provides an exe file to facilitate this. I have used this for one of my utility and worked perfectly. If you are interested I can send u the code.

- 1,288
- 16
- 19
-
You either mean a bash script, which won't work very often on Windows, or a batch script, which won't work very often off Windows. There are even systems which have Java but no scripting at all, like some mobile phones. – Olathe May 15 '13 at 01:05
-
Hi Olate the assumption was that the code will be running on windows so i meant batch script. The question did not specify as to whether it will run on Linux or windows. For u to say it wont work is absolutely ridicule as i have done this before. String cmd = "cmd /c " + [7Zip APP] + " x " + [ARCHIVE] + " -o" + [Destination]; Process process = Runtime.getRuntime().exec(cmd.toString()); 7 zip provide a app that u can use to extract and compress. Voting down my answer??? . This works perfectly on windows – Thakhani Tharage May 16 '13 at 11:33
-
It doesn't necessarily work perfectly on Windows. The user has to install the 7-Zip software without error in order to make it work, which is quite a hassle, particularly if the user is not at all good with computers (which is made even more difficult if they also don't have administrative rights; will they even know that the shell search path exists so they can set it up properly?). More importantly, there are much easier multiplatform solutions that don't require the installation of any additional software and that will work on Linux web servers and Android as well, where Java is used a lot. – Olathe Jun 01 '13 at 03:46
-
The questions never specified the platform, i am glad there is a platform independent jar. Though it was not necessary to vote down this solution as it works within the context of the question – Thakhani Tharage Jun 02 '13 at 06:37