Java - set path to file in zip - java

I have something like this:
File.txt/directory/directory/Main.java
and all of it is packed in a zip.
What is the correct path to File.txt in Main.java?

The short answer is that you cannot do this: there is (currently) no approach that allows you to point to a file inside a zip file. What you can do is read the zip file as a stream and then take the file that you need. For that, I would refer you to Read Content from Files which are inside Zip file.

Related

Read only file in Zip archive

I'm trying to generate a zip file with java.util.zip API and I haven't found any way to set zip entry as read only file. I would like to create a new ZIP archive, put files inside it and set read only flag for all those files that appear inside of the ZIP file (on Windows platform).
I am aware that java.util.zip API works with Streams instead of File objects (File object has method setReadonly()).
I also tried with Apache Commons Compress API and haven't found solution as well.
Please help!

programmatically creating links in Zip file - java

I am creating a zip file using ZipOutputStream. There will also be a manifest file (a csv file) which will have links to the entries in the Zip file. How do I programmatically create links for the zip entries ?
If you keep track of all the entries while you write them, you should be able to add another entry containing the "links" (but how should a csv link to a file? Please specify what you try to achieve).
If you intend to use the file under windows, you could create .lnk files programmatically; but this only works for one file per link. On unices, ZipOutputStream cannot create symlinks, but ZipFileSystem can.

Java remove part of file path

Using http://www.java2s.com/Code/Java/File-Input-Output/Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm slightly modified to zip directories up into a zip, I am left with this:
source path
E:someDir/someDir/somefile
and path in .zip
E:someDir/someDir/somefile
waht I would like to get in the .zip is
someDir/somefile
though, how to achieve this, if the full dir path will be varying between users of the program?
From the example from your post, this line is what needs to be modified:
out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
should be
out.putNextEntry(new ZipEntry("someDir/somefile"));
But then you should be able to derive someDir/somefile from the complete source path E:someDir/someDir/somefile. You should be able to do this either by substrings or by appending File.getParentFile()

How can I add a directory (tree of files) into a zip?

How can I add/modify/delete/merge recursive directory in a zip file (in Java) without file system?
Do I have to respect the order of zip entries?
Yes, I know merging directories is very complex job..
If you need to add whole directory with files to zip archive recursively only by Java core efforts, then you can use good example from Mkyong's blog. If you need to append files to existing zip-file, the you should use a link from #McDowell's comment: Appending files to a zip file with Java
There is no simple answer, your going to need to write a faire bit of code. You can't use the JDK ZipFile class, as that only supports reading zip files.
Instead use Commons Compress. Have a look at the examples and the zip documentation to get going.
Basically you'll need to open an input zip file, and an output zip file. Read each entry in tern, and decide whether to write it to the output, transform and write, add a new entry, or skip it, . When you get to the end close both zip files.
When processing a zip file, it's not really recursive, as all the entries are just a linear list with a path and filename. The recursive part comes when a zip contains a zip, and that is quite easy to handle.

Get contents of a directory in a ZIP file in Java

I have some default configuration files inside my application jar that I would like to save to the file system if they don't already exist. I would like it to keep the directory structure too. Example:
Jar file
-configs/
-main-config.cfg
-another-file.txt
-stuff/
-another-file.cfg
-com/
-META-INF/
I would like the contents of configs/ to be mirrored to the file system, including the subfolder.
Use JarFile.entries to get an enumeration of all of the entries in your Jar file.

Categories