ZipOutputStream relative paths - java

I create zip file using ZipOutputStream. I put in the zip one file(both file and zip are in the same dir), however the file is stored with fullpath (C:\TEMP\file.xml), how to store it with relative or no path?

You need to set that in the ZipEntry. For example, if you don't want any path, just use the name of the file in your ZipEntry, like this:
File f = new File("C:\\temp\\file.xml");
ZipEntry entry = new ZipEntry(f.getName());

Related

Replacing files in multilevel zip folders

My requirement is to replace few files in a zip file.
The zip file in turn have multiple zip files and folder within it. It goes upto 4 or more levels of zip files.
I have a set of files in a different source directory. I would like to copy these files and replace within the zip file, by matching the file name in the source directory with file name inside the zip file.
Could someone help here please.
Thanks,
Deleep
One way is use temp files to store the ZIPs and open them as FileSystems, so you dont need to extrat all files and rezip everything
FileSystem firstZip = FileSystems.newFileSystem(URI.create("jar:file:/root/firstZip.zip"),
new HashMap<>(), null);
//Get a zip inside the first one
Path path = firstZip.getPath("FOLDER/ZIP_NAME.zip");
//Get the second zip input stream and store the zip in a temp file
InputStream in = firstZip.provider().newInputStream(path);
Path secondPath = Paths.get("/root/temp/tempFile.tmp");
Files.copy(in, secondPath);
//open the second zip
FileSystem secondZip = FileSystems.newFileSystem(new URI("jar:" + secondPath.toUri().toString()),
new HashMap<>(), null);
//iterate files in the second zip
DirectoryStream<Path> ds = Files.newDirectoryStream(secondPath);
for(Path p: ds){
//something
}
//delete or update files inside the second zip
//Files.delete(seconZip.getPath("aFile.txt"));
//Files.copy(anInputStream, secondZip.getPath("destFoldet/aFile.txt"));
//clse the seconzip
secondZip.close();
//update the second zip inside first one
Files.copy(secondPath, firstZip.provider().newOutputStream(path));
//delete temp file
Files.delete(secondPath);
//close first zip
firstZip.close();
Other idea is jboss-vfs. Never tried it, but i think that could help.

how to save file path after move it from a directory to another in java

I copy a file from a directory for another but I have a difficulty to find the new file path !
I used FileUtils class from apache commons-io library to do that..... please is that there a function can save the last file path ?
Since FileUtils.moveFile accepts two arguments -- source file and destination file, all you need to do is to use second arg:
File myFile = new File("file");
File newLocation = new File("funky_file");
FileUtils.copyFile(myFile, newLocation);
myFile = newLocation;
You cannot retrive new location basing only on myFile without reassigning: File class is designed to be a immutable path rather that hard link to the file.

Creating a series of Folders in sdcard android

My require file in sd card has path
Android/data/mypakagename/android/myfile/abc/myfile.txt
i have a url from where i will download this myFile.txt and place it in this location.
Right now I have directory Android/data/mypakagename/ created in sdCard.
Now i am using following code.
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
file.mkdirs();
This creates all folders however it also create myfile.txt as a folder.I want to create all missing directories except last one. How can i do that?
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
File parent = file.getParentFile();
parent.mkdirs();
String filename = "myfile.txt"
File dir = new File ("Android/data/mypakagename/android/myfile/abc");
dir.mkdirs();
File file = new File(dir, filename);

java: How to copy a directory in a jar file to a tmp dir?

I know how to create a temporary directory in Java, but is there an easy way to copy files in Java from the jar file to this directory?
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
File helpDir = new File(tmpDir, "myApp-help");
helpDir.createNewFile(); // oops, this isn't right, I want to create a dir
URL helpURL = getClass().getResource("/help-info");
/* ???? I want to copy the files from helpURL to helpDir */
Desktop desktop = Desktop.getDesktop();
URI helpURI = /* some URI from the new dir's index.html */
desktop.browse(helpURI);
Apache's org.apache.commons.io.FileUtils can do that for you
To create directory use File.mkdir();
Convert URL to File with org.apache.commons.io.FileUtils.toFile(URL)
use org.apache.commons.io.FileUtils.copyFile() to copy.
You can make use of the command jar tf [here your jarfile]. This will list the contents of the JAR Archive with their full path, relative to the jarfile (1 line = 1 file). Check if the line starts with the path of the directory you want to extract, and use Class.getResourceAsStream(URL) for the matching lines and extract them to your temporary folder.
Here is an example output of jar tf:
META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
images/
images/cross.gif
images/not.gif

Setting path X in "ZipFile zipfile = new ZipFile("X");"

I'm having problems setting the path of the zip file, X, in ZipFile zipfile = new ZipFile("X");.
I don't want to hardcode the path such that it becomes ZipFile zipfile = new ZipFile("C:/docs/data.zip");.
I want to do something like :
ZipFile zipfile = new ZipFile(getServletContext().getResourceAsStream("/WEB-INF/" + request.getAttribute("myFile").toString());
Where the path of the zip file is determined by the selection of the user. But, this gives an error, because this only works for InputStream.
Previously, I've already retrieved the multipart/form data and gotten the real path of the zip file:
String path = getServletContext().getRealPath("/WEB-INF");
UploadBean bean = new UploadBean();
bean.setFolderstore(path);
MultipartFormDataRequest multiPartRequest = new MultipartFormDataRequest(request);
bean.store(multiPartRequest); //store in WEB-INF
// get real path / name of zip file which is store in the WEB-INF
Hashtable files = multiPartRequest.getFiles();
UploadFile upFile = (UploadFile) files.get("file");
if (upFile != null) request.setAttribute("myFile", upFile.getFileName());
Any solutions to this?
You can convert webcontent-relative paths to absolute disk file system paths in two ways:
Just use ServletContext#getRealPath() as you previously already did.
ZipFile zipfile = new ZipFile(getServletContext().getRealPath("/WEB-INF/" + request.getAttribute("myFile").toString()));
Use ServletContext#getResource() instead. It returns an URL. Call getPath() on it.
ZipFile zipfile = new ZipFile(getServletContext().getResource("/WEB-INF/" + request.getAttribute("myFile").toString()).getPath());
Way #1 is preferred.
I don't understand why you don't use the real path that you already have.
Anyway, you can work with a ZipInputStream.
This way you can handle your file as a simple Stream. The only big differences are the getName() method and size() that you can't directly access. With a ZIS you will be able to read every entry.
Resources :
Javadoc - ZipInputStream

Categories