This question already has answers here:
What's a good compression library for Java?
(3 answers)
Closed 9 years ago.
I have a problem. Currently I'm developing video game in java language. At this moment data files take about 100MB space even though game world is not big. I want to zip those text files ant protect them with password or some kind of encryption, but I can't find any good and free library for that.
Or maybe it's possible to pack data into some kind of archive without external libraries?
Update
I tried to download Zip4j, but it shows that I need source attachments and I can't find in library's site.
You can compress yor game data with the facilities provided by the JDK:
java.util.ZipFile and related classes to handle normal zip files
java.util.GZIPInput/OutputStream to compress data directly (no "files catalog" concept, just a blob).
java.util.DeflaterInput/OutputStream (like GZIP)
There exists a multitude of 3rd-Party compression classes. I personally like XZ for Java, because it provides excellent compression ratios and is easy to use through the standard stream interface (http://tukaani.org/xz/java.html).
For encryption, there are encrypting streams that are used just like the compression streams. Beware that anybody will be able to extract the "key" from your game files with a little knowledge and patience if they really want to.
You can use apache Compress Library for compressing file.
http://commons.apache.org/proper/commons-compress/
you can also compress using zip libraries at java.util.zip
Java provides several features that compress the data such as:
GZIPInputStream and GZIPOutputStream
you can also use zip with ZipFile (java.util.zip)
For encryption you could write your own FilterOutputStream and FilterInputStream where you use a Cipher to encrypt it.
Those features are working all without external libraries.
Related
I'm currently trying to build a function for hunting down files in nested zips. The general problem I can handle and I've got my code working correctly. The problem I'm having is related to compression formats.
Default zip files are not an issue, I open my zipinputstream, send it to my search function, check for each of the entries if it is a zip file and if so I wrap the inputstream in another zipinputstream and recurse. The problem comes when I have a non standard zip compression.
I would like to make this function much more robust: to be able to detect the compression format of the zip entry, wrap it in the correct deflater, and then wrap that in a zipinputstream so that I can recurse. The problem is I don't know how to decode the compression settings. I also don't know how to handle other zip compression formats.
Any advice on how to make this function more flexible would be helpful. I intend to integrate this into my own personal libraries so will be using this function from all sorts of directions in the future.
As far as I know, the only compression algorithm supported by java.util.zip is the standard one. But you can detect the compression algorithm using the ZipEntry getMehod() .
Adding support for other compression algorithms may require some work on your part - unless you can find a library with support for different algorithms. For instance, there is a library for 7zip's LZMA format, but the classes do not extend Decoder or ZipEntry - they just provide an alternative. You would need to extract the nested zip file uncompressed and then use something else to unzip it. This is probably not as clean as you would like.
Is there a simple way, either through the Java libraries or a third party library to zip an existing file in Java?
I am already familiar with the approach of creating a ZipOutputStream, adding ZipEntry objects to that, and then reading the data from a stream into the ZipOutputStream, I'm looking for a simpler way to zip up one File. Most likely this is going to be a recommendation for a third party compression library.
The Apache Cayenne project has a pretty simple ZipUtil, you can check out the javadoc here:
http://cayenne.apache.org/doc20/api/cayenne/org/apache/cayenne/util/ZipUtil.html
Looking at the source it only has imports from the Java SDK so it should be easy to just drop into your application:
http://svn.apache.org/repos/asf/cayenne/main/branches/cayenne-jdk1.5-generics-unpublished/src/main/java/org/apache/cayenne/util/ZipUtil.java
I have a database which contains picture data stored as a binary blob. The documentation says the data is encoded using LZW. I thought that I could decode it using the Zip or GZip input streams found in the Java library, but it didn't work - I got an exception that said the format of the data is not correct.
From what I've read, the library uses DEFLATE, which is not LZW. Also, I've read about some licensing problems for using the LZW algorithm.
What can I use to decode the data? Is there a library? Do I have to implement it myself? What about the licensing problems?
I know the question is old, but I just wanted to add a great resource about LZW:
http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp
It's more specifically about the use of LZW in GIF images, but it explains the compression and decompression algorithms pretty well.
Here are a couple of links:
http://www.cs.sfu.ca/CC/365/li/squeeze/LZW.html
http://u.cs.biu.ac.il/~freskom1/AlgProg1/Progs/LZW.java
http://www.codeproject.com/KB/java/lzw.aspx
And there are others.
Indeed if the images are LZW compressed TIFF files, The Java Advanced Imaging API apparently supports decoding directly (though not encoding it seems).
You can also try with 7-Zip JBinding which uses the 7zip library internally. It's quite easy to use.
I went through a surprising amount of LZW implementations before finding one that worked for my case.
UncompressedInputStream from the BioJava project worked for me, when I needed to unpack a .pax file.
We have a requirement where we need to convert from .wav file to .mp3 and we are currently using "Tritonus" library to do that . The concern with that library is that requires "installation" of some "dll" files to the class path.
I am wondering are there any API's those allow better processing without local installation.
And other question is ,having mp3 format files will make it easier to join the files into a single file than having .wav files ?
As a former contributor to the JLayer MP3 Library, I'm fairly sure that it doesn't do WAV to MP3 - just MP3 playback and conversion to WAV. (I spent some time optimizing the decoder :-)
Regarding appending files (and possibly other operations), it is generally better to perform edit operations using the uncompressed format, and compress at the end.
I think the spec allows mp3 files to be concatenated, since they are a series of frames, but behaviour may vary from player to player.
So, to be safe, and maintain quality, I'd concat using WAVs and then compress the final result to MP3. Concating files is not so straightforward - you have to at least make sure they are at the same percieved volume, or you will get a noticible shift in volume from one file to the next. Such operations are definitely best performed on the uncompressed data.
The JLayer MP3 Library appears to support several operations on MP3 and WAV files including conversion, with no native libraries to install.
You can use MP3SPI to do this. This is a java sound plugin, just include the jar into the classpath, and you can use java sound api to convert between wav and mp3.
If i compress and decompress data using java then it works fine
My if friend uses C#.NET to compress data while i am using java SO inthis case I can not decompress the same data.
I am using inflatter and deflater in java.
Thanks
Bapi
Use GZIPInputStream and GZIPOutputStream which are compatible with gzip format.
Note: each compression format is different (though similar in approach) and they have to be the same to work.
Inflator and Deflator are a cut down version of the GZIP format and I wouldn't expect it to work with anything another other than Java.
The java.util Deflator and Inflator support are the basic zip compression and decompression format implementations - they do not create a free standing archive file. Rather they create/read the data stream which corresponds to the data portion of one entry in an archive file.
To create an archive file readable by general compression utilities use either the java.util.GZIPXxx or java.util.ZipXxx classes.
Thanks for your support.
I changed my compression logic. I used GZIPInputStream both in java and in C#.NET
after this only. my problem is solved
Thanks
Bapi