I have stored blobs of multiple files in the database. User should be able to click on a link and should be able to download all the files as zipped. But before that I want to provide a folder structure to group those files. Say, a base folder followed by nested folders and related files. I followed the link below, looks like, I need to first create a physical dir structure for the same and then zip that structure?
http://www.journaldev.com/957/java-zip-example-to-zip-single-file-and-a-directory-recursively
Files in a zip directory are abstracted as ZipEntry objects in Java. Those have names that corresponds to the path relative to the directory where you unpack the zip file. Directories in a zip file are simply entries whose name ends with /. This way, you can also view a zip file as a plain list of binary data entries whose names reflect the directory structure.
So you don't have to create a folder structure on you hard drive and zip it but you can write directly from the database to the ZipOutputStream:
Create each folder as a ZipEntry with no content and a name ending with /.
Instead of using a FileInputStream for reading from files on the hard drive, write the output of resultSet.getBinaryStream("blobcolumn") directly to the entry.
Related
I am trying to read and parsed all the CSV files, these files are inside of extracted zip file, but unfortunately, I'm only getting this (No such file or directory) - How can I get inside that folder and read those files? Take note that this folder is already unzipped.
I'm also thinking, that if that is not possible - is there any way that I can extract the zip file to files only, for example below:
Zip File: ZipFolder.zip
Expected: (No folder at all, automatically the files is on the target location already).
CSVFile1.csv
CSVFile2.csv
Can someone help me to get through this? your help is appreaciated.. Thank you!
I would like to get a list of file contained in a directory which is in a jar package.
I have an "images" folder, within it I have an Images class that should load all images from that directory.
In the past i used the MyClass.class.getResourceAsStream("filename"); to read files, but how do I read a directory?
This is what I tried:
System.out.println(Images.class.getResource("").getPath());
System.out.println(new File(Images.class.getResource("").getPath()).listFiles());
I tried with Images.class.getResource because I have to work with File and there isn't a constructor that accepts an InputStream.
The code produces
file:/home/k55/Java/MyApp/dist/Package.jar!/MyApp/images/
null
So it is finding the folder which I want to list files from, but it is not able to list files.
I've read on other forums that in fact you can't use this method for folders in a jar archive, so how can I accomplish this?
Update: if possible, i would like to read files without having to use the ZipInputStream
You can't do that easily.
What you need to do:
Get the path of the jar file.
Images.class.getResource("/something/that/exists").getPath()
Strip "!/something/that/exists".
Use Zip File System to browse the Jar file.
It's a little bit of hacking.
I'm tried several ways to zip a directory structure in a zip file with Java. Don't matter if I use ZipOutputStream or the Java NIO zip FileSystem, I just can't add empty folders to the zip file.
I tried with unix zip, and it works as expected, so I discarded a possibly zip-format issue.
I could also do a little workaround, adding an empty file inside the folders, but I don't really want to do that.
Is there a way to add empty folders in zip files using java API's?
EDIT: Based on answers and comments, this is pretty much the solution I got.
Thanks!
Java NIO makes this as easy as working with a normal file system.
public static void main(String[] args) throws Exception {
Path zipfile = Paths.get("C:\\Users\\me.user\\Downloads\\myfile.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(zipfile, null);) {
Path extFile = Paths.get("C:\\Users\\me.user\\Downloads\\countries.csv"); // from normal file system
Path directory = zipfs.getPath("/some/directory"); // from zip file system
Files.createDirectories(directory);
Files.copy(extFile, directory.resolve("zippedFile.csv"));
}
}
Given a myfile.zip file in the given directory, the newFileSystem call will detect the file type (.zip mostly gives it away in this case) and create a ZipFileSystem. Then you can just create paths (directories or files) in the zip file system and use the Java NIO Files api to create and copy files.
The above will create the directory structure /some/directory at the root of the zip file and that directory will contain the zipped file.
I have a series of zip files and I want to add their contents to another zip file. How could I do this without extracting? I can't extract a file inside one of the zips because it is called aux.class, which cannot be made on Windows.
The problem is, Zip files weren't designed to be appended to.
What I would try and do is
Create a new zip file
Read the contents of each existing file, writing the contents of the stream to the new zip file
Rename/delete the old zip (the one you are trying to "append")
Rename the we zip ino it's place
You could have a look at Compressing and Decompressing Data Using Java APIs for some ideas
I have tow folders, say folder1 and folder2.
folder1 contains files of different type like .txt, .doc, .png. All file are posted in this folder only.
Now, what i want to do is as soon as file is saved in folder1, a copy of that file goes into subfolder in folder2 and the subfolder name should be the extension of that file. i.e if abc.txt file is saved in folder1 then txt folder should be created inside folder2 (if txt folder does not exist) and abc.txt should be copied inside that subfolder. And if we have xyz.doc file then create doc folder and save inside that. I want to do this in Java.
and this should be platform independent i.e we can copy in Window, Linux or Android.
You can achieve this one by File.list() & Common's FileUtils
You can start a thread that checks frequently for new files in that folder. The thread has a list of all hashes from the files. If the file is not in the list its a new file. Then you can copy the file to the new location. Instead of checking the name of a file you can check the hash.
Can you wait for Java 7? WatchService is perfect for notification of new File objects.
Quote from the top of those JavaDocs:
A watch service that watches registered objects for changes and events. For example a file manager may use a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.
( Emphasis by Oracle, boldness by me. ;)