Adding empty directories to .tar file in Java using JTar - java

I'm trying to package a directory, which includes an empty sub-directory, into a .tar file using Java's JTar. Folders that contain files are automatically included in the .tar file, but the empty folders are not. I want to preserve the directory structure below, is this possible?
MyTarFile.tar--|
|--ChildFolder1--|
|--MyFile.txt
|--ChildFolder2--|
ChildFolder1 is created, ChildFolder2 is not.

Related

Writing to text file created manually inside app

Is it possible in Android, to manually add a file inside a project and then, modify it? Example:
"I have a test.txt file in the following path: "app/src/data". I would like to make a method to write a given String in the test.txt file."
Is that possible? I been looking everywhere, but can't seen to do such an easy task.
If you mean modifying files inside the APK itself then it's not possible. Besides, the folder structure you see in the project is not the final structure on the APK (just unzip your APK, it's a .ZIP really): Fpr example, the source directory is all compiled into a classes.dex. The res/ directory is compiled and fully copied ...
Take a look at How to write files to assets folder or raw folder in android?
and https://developer.android.com/training/basics/data-storage/files.html
You can read raw files stored in /res/raw, or assets stored in assets/ , but you cannot modify stuff inside the APK itself.
What you can do is create and modify as many files as you wish from the different places Android gives to any app, such as:
CACHE directory (context.getCacheDir() -> /sdcard/Android/data/your.package/cache
External files (context.getExternalFilesDir() -> /sdcard/Android/data/your.package/files
Arbitrary directories in the SDCARD.

Zipping single file in a single zip without creating a folder

In java most of the zipping apis zips the single file by creating a folder.
For example in windows if we choose to right click-->Winzip-->Add to selected_file_name.zip it creates selected_file_name.zip and if we extract we directly get file rather than a folder and within folder a file.
After extracting by selecting a extract to here only file should come rather than a folder and then a file within that.
Can we do the same using any java api ?

How to stop creating thumbs.db files in compression using java

I am trying to compress a folder to zip and extract the same folder using java. When i zipped or unzipped the folder the files which are in the folder compressed and extracted successfully. But a Thumbs.db file is creating with in the folder.
I want to delete/avoid the Thumbs.db files before/after compressing to zip or extracting from zip programmatically.
Is it possible in java ?
Thumbs.db is usually a file with the system and hidden attributes (see attrib.exe ) and therefore invisible in the Explorer (unless you set the Explorer to show all files including system files).
When you extract the file you should set it's attributes accordingly.
How to set a file attribute using Java is already described here: https://stackoverflow.com/a/36465283/150978

Java listFiles in directory in jar

Is there any way to use listFiles() on a directory that's been packaged into a jar?
Let's say I have a directory in my resource directory with some text files: texts/text1.txt and texts/text2.txt.
And within this Java program I have a class that needs to use listFiles() to get a list of those files. I'll get something like jar:file:/home/soupkitchen.jar/!text. I'd expect that to be a directory. Is there any way to be able to treat it as a java.io.File directory containing files? Right now it seems to only be listed as neither a file nor directory.
No. java.io.File can only be used to list real directories.
However, you can treat it as a java.nio.file.Path.
Overall, you have three options:
Open the .jar as a Zip File System and use Files.newDirectoryStream or Files.list.
Iterate through all entries in the .jar file, looking for names that match.
Put a text file in your .jar that contains the names of all the entries in the directory, so you don't have to try to list them.

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