Zipping single file in a single zip without creating a folder - java

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 ?

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.

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

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!

Creating and executable JAR file that uses an external XML file

I have a java application in Eclipse that uses an eml file like so
File matches = new File("matches.xml");
The file is located in the default package as all the other classes. When I create the JAR it bundles in the XML file with it. My application require me to be able to make changes to the XML file. How can I set it up so the JAR can reference the XML file outside of itself?
My application require me to be able to make changes to the XML file.
Then you will need to extract it from the Jar and save it somewhere on the local file system.
See How can an app use files inside the JAR for read and write? for more details.
If you're using new File("matches.xml") that won't use a file within a jar file at all. It will only look on the external file system.
If you need to be able to use an external file if it's present, or the version in the jar file as a fallback, you'll need to test for the file's existence (File.exists()) and use Class.getResourceAsStream("matches.xml") for the fallback behaviour.
As you want to keep the file outside the jar and want to update it so the jar can read, so you can put the file in the same directory where the jar is and use the following code to access the file
FileInputStream file = new java.io.FileInputStream("matches.xml");
So this can be the directory structure.
- matches\
- matches.jar
- matches.xml

finding static file in jar

I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored a csv file in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource(ApplicationConstants.ALIASESFILE).getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, it gives a path as shown below
D:\javaProjects\AutomodeGS_Prachi\lib\internal\RESTWSGS.jar!\aliases\aliases.csv
I am getting a file notfound exception.What could be wrong?
The getResource() method is returning a "jar:" URL. The path component of that URL is not a normal filesystem pathname, and can't be opened directly using Java's file classes.
The simple way to do this is to use Class.getResourceAsStream(...) to open the stream. If you need an "identifier" for the JAR entry, use Class.getResource(...), but then open the stream using URL.openStream().
This works fine from glassfish may be because glassfish has exploded jar on file system so that your csv file is acutually a file to the file system,
if you try to read it from another project it fails because the jar containing your file is in classpath that is fine, but the csv file is under jar file and it is no longer a File
You can read it as Stream
InputStream is = MyClass.class.getResourceAsStream(ApplicationConstants.ALIASESFILE);

Categories