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
Related
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.
I has a excel file with 4 excel sheets in it. Now i want to read or write to required excel sheets using java without using any third party lib.
I know i can read and write data using FileInputStream and FileOutputStream respecitvely. But i can handle the work sheets??
No, you can'not, There is numerous way in Java for reading/writing files, but there is no built-in support for MS Office/Excel spreadsheets. http://poi.apache.org/ - is a key to victory.
If your goal is to interface with data from an excel sheet from your Java application, I'd suggest to use the solutions suggested by other posters, it will save you a lot of work.
If, however, you want to be able to read excel files from Java (or any other programming language for that matter) 'just because you can' then you could take a look at this file and read the instructions on this web-page. I would warn you that it would take considerably more time to implement your own API if you base it only on the file-specs that are publicly available. You might want to check out the work done by the people from the Apache POI project to get an idea of how to approach it. Or (even better) contribute to the project. Here you can find out how to go about doing that
Is there any java library by which we can compress a plain text file(.txt) into winRAR format(.rar). I have been searching in google but couldn't find any relevant library which does that.
you can always do
Runtime.getRuntime().exec("rar -a somefile.txt");
I think RAR is not open-source format. I'd rather use ZIP - it's built-in in Java.
RAR is a proprietary format, and I don't know of any library to create it (although there seem to be some that can read it). Any reason why you wouldn't use another, more open format like 7zip (for compression), or plain Zip (for universal compatibility)?
(From the question, of which this one is a duplicate - RAR archives with java)
You could try JUnRar, "a RAR handling API implemented in pure Java" (quoting the site).
No you can not do it. Only WinRAR can create rar file. There is only a unrar library for it.
One reason to use RAR: It is sooooo much better than ZIP. For example: One of our production print files has a lot of repetition (including images) in it. 142 MB large, it zips to about 80MB. RAR seems to detect these repetitions and the resulting file is not even 2MB!
Why RAR? as others have mentioned, RAR is not open.
Java has native ZIP API.
Java Zip API: How to Create a Zip File Using Java
I'm facing a problem that, we have a .zip file that contains some text files. Now I'm using java to access that files. If it is not in the .zip file I can read and print on my console easily using FileInputStream.
But how to read a file from .zip file? I use J2SE only..
You should try a ZipInputStream. The interface is a little obtuse, but you can use getNextEntry() to iterate through the items in the .zip file.
As a side note, the Java class-loader does exactly this to load classes from .jar files without extracting them first.
Everything you need is in ZipFile: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html. Google for examples on the web, and if you have specific problems then come back to SO for help.
(The link will eventually break; when it does simply websearch java zipfile.)
I'm trying to create a Zip file from .Net that can be read from Java code.
I've used SharpZipLib to create the Zip file but also if the file generated is valid according to the CheckZip function of the #ZipLib library and can be successfully uncompressed via WinZip or WinRar I always get an error when trying to uncompress it using the Java.Utils.Zip class in Java.
Problem seems to be in the wrong header written by SharpZipLib, I've also posted a question on the SharpDevelop forum but with no results (see http://community.sharpdevelop.net/forums/t/8272.aspx for info) but with no result.
Has someone a code sample of compressing a Zip file with .Net and de-compressing it with the Java.Utils.Zip class?
Regards
Massimo
I have used DotNetZip library and it seems to work properly. Typical code:
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddDirectory(sourceFolderPath);
zipFile.Save(archiveFolderName);
}
I had the same problem creating zips with SharpZipLib (latest version) and extracting with java.utils.zip.
Here is what fixed the problem for me. I had to force the exclusion of the zip64 usage:
ZipOutputStream s = new ZipOutputStream(File.Create(someZipFileName))
s.UseZip64 = UseZip64.Off;
Can't help with SharpZipLib, but you can try to create zip file using ZipPackage class System.IO.Packaging without using 3rd part libraries (requires .NET 3+).
To judge whether it's really a conformant ZIP file, see PKZIP's .ZIP File Format Specification.
For what it's worth I have had no trouble using SharpZipLib to create ZIPs on a Windows Mobile device and open them with WinZip or Windows XP's built-in Compressed Folders feature, and also no trouble producing ZIPs on the desktop with SharpZipLib and processing them with my own ZIP extraction utility (basically a wrapper around zlib) on the mobile device.
You don't wanna use the ZipPackage class in .NET - it isn't quite a standard zip model. Well it is, but it presumes a particular structure in the file, with a manifest with a well-known name, and so on. ZipPackage seems to have been optimized for Office docs and XPS docs.
A third-party library, like http://www.codeplex.com/DotNetZip, is probably a better bet if you are doing general-purpose ZIP files and want good interoperability.
DotNetZip builds files that are very interoperable with just about everything, including Java's java.utils.zip. But be careful using features that Java does not support, like ZIP64 or Unicode. ZIP64 is useful only for very large archives, which Java does not support well at this time, I think. Java supports Unicode in a particular way, so if you produce a Unicode-based ZIP file with DotNetZip, you just have to follow a few rules and it will work fine.
I had a similar problem with unzipping SharpZipLib-zipped files on Linux. I think I solved it (well I works on Linux and Mac now, I tested it), check out my blog post: http://igorbrejc.net/development/c/sharpziplib-making-it-work-for-linuxmac