Using zip file without extracting in java - 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.)

Related

How to decode and read a chm file in java?

I see many suggestions to use the jchm jar to do this. However I don't see any code which shows how to read the chm file using the jchm jar.
I used hh.exe -decompile [destination_folder] chmfile.chm to decompile the .chm into its underlying .htm files. I am now writing java code to parse all these .htm to create a tree structure to store the complex structure of hyperlinks in the .chm file.

Two applications need to export and import a single file which needs to include data and images, best file type?

I'm making two Java applications one to collect data, another to use it. The one collecting will be importing a file from the other which will include data and images and will be decrypted.
I'm unsure what filetype to use. So far all of the data is in XML and works great but I need the images and was hoping not to have to rely on giving all the images in a folder with a path reference.
Ideas?
well, I think that the best way is to create your own format (.myformat or .data). This file will be in fact a Zip file that contains your XML file and images.
There is no perfect example writen in java as far as I know. However, here are some examples :
Not in java
The best example is, as #Bolo said, the odt format. Indeed, OpenOffice writes the doc in an xml file, and the images too. All that is wrapped in an odt file.
The .exe file is an other example. The C files and the resources are put in a single file. try to open it with 7-zip, you'll see.
The Skyrim plugins are .esp file that contain the dds, the scripts, the niffs (textures)...
In java
The minecraft texture packs are a zip file that contains a .mcmeta file (the infos) and the textures (.png)
Jar files are like exe.
If both programs are in java you could also go with serialization, which is basically saving an object as a file (suffix will be .ser I think) and then being able to retrieve it. You should google it, even if it won't help right now it is quite good to know about it.
I'd suggest using JSON. Gson is a decent library.
You can embed images as byte arrays.
Save the serialized string in a file with a preferred extension, read it from the second application, de-serialize, and reconstruct images.
You can convert binary image data to text with Base64 encoding and this way you can embed your images in XML. [1]: http://en.wikipedia.org/wiki/Base64

How does one access a file within a directory in a zip file without creating any new files?

I'm working on a java project that requires me to access a file within multiple embedded zip files and directories.
For example, archive1.zip/archive1/archive2.zip/archive2/directory1/file_that_I_need.txt.
It would be a lot easier if when each zip file was extracted, it would immediately list its contents but instead there's a folder inside that contains all the contents.
The examples I found online deal with zip files that, when extracted, contain the files they need to access but I can't find any that deal with accessing files within a directory in a zip file. Any advice on this would be great.
Thanks!
Given the prohibition against creating new files, you're pretty much stuck with ZipInputStream. When you find the ZipEntry that corresponds to the embedded archive, you then read its stream to find the actual file. You can proceed recursively through as many levels of archives as you want.
This works OK if you're looking to process a single file. However, re-reading the archives for multiple files can be expensive. A better solution is to at least open the outer archive as a ZipFile, which memory-maps the actual file.
If you can then extract the contained archives into a temporary directory and open them as ZipFiles as well, you'll probably see a big speed increase (as long as you're pulling multiple files from each embedded archive).
You might also look at http://truezip.java.net/ I've used an older version of it, and its quite a bit more powerful than the support that's built into Java. I think there is also an Apache Commons library for reading files from within nested archive structures.

How to compress text file to rar format using java program

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

Help in creating Zip files from .Net and reading them from Java

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

Categories

Resources