Java remove part of file path - java

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()

Related

Java can't get relative path for file

Image DescriptionTrying to access a test.txt file that is in the same location as my HelloController.java file but for some reason, it is showing that the file does not exist. I've tried moving the file around but it does not work.
Using the absolute path works, but this is a shared project so it will be ran on other computers. Any suggestions would be much appreciated.
Your best bet is to add it to the class path and reading it as a class path resource.
The relative path root is your "working directory". Which means if you try to access "." you will start at your working directory. This directory is only set once for your application and is normally the folder which was opened when you started it.
When working with IDEs (like in your case) the working directory will be the root folder of your project (So the folder in which the pom.xml and src folders are located.
If you want to access the file via the normal file API you are currently using, just put the file in that diretory and it should work (given you share it with the other people in the same location).
If you need the file to be inside your generated output jar-file, you will need to use the File as a resource (See duffymo's answer), as the file does not exist by itself on the file system, but as a file inside your jar-file.
If you want to know your current working directory, you can create a File refrence to "." and expand it to an absolute path (Which will replace refrences like "." and ".." and generate a file path from your root) and then write it to the console. This would look something like this:
// Get refrence to the current working directory
File workingDirectoryReference = new File(".");
// Convert it to an absolute path string
String absolutePath = workingDirectoryReference.getAbsolutePath();
// Output to console
System.out.println(absolutePath );

Java - set path to file in zip

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.

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.

Java & JTar - how to add a file inside a folder using JTar?

Windows - I am trying to create a new Tar file with with the JTar lib with the following inside...
MyTarFile.tar--|
|--MyFolder--|
|--MyFile.zip
I can create it with the folder and the zip file right in the root of MyTarFile but I don't know (and I looked around) how to create that folder AND have the zip file inside. I need to know what to use (the File object(s)) for the TarEntry(s) (is it one for folder and one for file...or one for both?) and what the InputStream should look like (I believe just a single one for the zip file but not sure). I am trying to create a file to mimic an existing format so I don't have the option of just losing that folder as the software that uses the file will be looking for it. I can add the zip file to the MyFolder folder on the actual file system (again, this is on Windows) before tarring or not...whatever works is fine.
I have tried full paths and relative paths (seems the InputStream MUST have a full path though) with no luck. Running out of ideas other than switching libraries (perhaps JTar doesn't support this).
Thanks!
Without seeing what you have already written, here is my best attempt at answering. I am unfamiliar with JTar, but after taking a look at the example on their main page, I wrote a quick test program that created a tar with one file in the root of the tar and one file in a directory in the tar, which I believe is what you are attempting to do. The code of interest to you is this:
TarEntry tarEntry = new TarEntry(new File("/Users/userGuy/Documents/students.xml"),"students.xml");
TarEntry otherTarEntry = new TarEntry(new File("/Users/userGuy/Documents/students2.xml"),"inner-dir/students2.xml");
Note that the second tar entry, otherTarEntry is instantiated with a relative path as the entryName argument in the TarEntry constructor. This is a poorly named argument, as it is technically the path of the file in the tar, not just the name.
With your example file names above, your code might look something like this:
TarEntry tarEntry = new TarEntry(new File("<path to file>"),"MyFolder/MyFile.zip");

Get path of a file from a subdirectory of a directory in CLASSPATH

Given the following directory structure:
working-directory
subfolder1
file1.wav
file2.wav
file3.wav
subfolder2
file4.wav
file5.wav
file6.wav
subfolder3
file7.wav
file8.wav
file9.wav
jar-that-im-running.jar
I need to get the path to each wav file. I figured that since the folder is in the working directory, and is thus part of the classpath (if my assumption is wrong, I'd just add the working directory to the classpath), I could just run:
String path = ClassLoader.getSystemResource("file1.wav");
or
String path = ClassLoader.getSystemResource("/file1.wav");
but it wouldn't work, unless I specified the folder the wave file was in. This would be fine, but I wouldn't know what folder each wave file is in; I only know their names. I'm going to process all the files one way or another, but the order that I do depends on a config file. Also, I am not going to edit these files directly. Instead, I'm going to be passing them off as arguments to a ProcessBuilder. Since some of the directories in the path may have spaces, which get converted to %20 in URLs, I figured I could convert them with path.replaceAll("%20", " "). Will I be better off using files, or is there a way to get a specific wav file, without knowing its parent folder.
Did you try getting from the class loader as a system resource? Here's a snippet of code to illustrate:
String path = ClassLoader.getSystemResource("subfolder1/file1.wav");

Categories