Android: ZipFile, FileNotFoundException when use Assets Files - android

I have a problem.
I use this code to read the content of a zip file:
File file = new File(TogglesManager.EXTERNAL_STORAGE_THEMES_DIRECTORY+"filename.zip");
ZipFile zip = new ZipFile(file);
But when I try to use an assets I have a FileNotFoundException.
To read asset zip file I use this code:
File file = c.getFileStreamPath("assetsFile.zip");
ZipFile zip = new ZipFile(file);
The "file" is not null, because if I write file.getName(); I see the correct file name.
I don't want to use ZipInputStream class, but only ZipFile

You cannot access Assets using normal file operations. You will have to use
AssetManager assetManager = mContext.getAssets();
InputStream is = assetManager.open("assetsFile.zip");
But since ZipFile does not take InputStream as a parameter, you will have to copy asset file onto internal storage and then use File
Further AFAIK, apk is zipped, so storing a zipped file inside assets might not change storage used by much, so maybe you donot need to use zipfile inside assets.

Related

How can I write a file in res directory?

I want to store a .json file within the res directory. How should the path be defined?
FileWriter file = new FileWriter("path_here");
You can't! Resources are read-only at runtime.

How to get access to application classes.dex file

I believe that every application can get access to its own files, for example - to classes.dex file.
I wonder how the application get access to its files?
and another question, can I fake this access and get access to another application.?
you can point to the apk file of your application using ApplicationInfo.publicSourceDir
File yourApk = new File(this.getApplicationInfo().publicSourceDir);
//create the destination dir
File tempDir = new File(getFilesDir(), "temp_dir");
//and then unzip the apk to that dir
unzip(yourApk , tempDir);
//now the tempDir will contain all the resources including the dex file and its accessible
this file is a zip file you can decompress that file using this link
How to unzip files programmatically in Android?
so you will have the access to all resources plus the classes.dex
hope this will help

Reading an encrypted zipped .obb file

I have two files (for example) that I want to put in my apk main expansion file. Let these files be img1.jpg and img2.jpg. I compress them using the following command:
zip -n .jpg main.123.in.example.app.obb *.jpg
(The version code is 123 and the package is in.example.app). This creates an obb file: main.123.in.example.app.obb
I place this code on an appropriate path. I can access the files img1.jpg and img2.jpg easily by calling the following:
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + "/Android/obb/in.example.app");
String pathToObb = expPath + File.separator + "main.123.in.example.app.obb";
ZipResourceFile zip = new ZipResourceFile(pathToObb);
InputStream iStream = zip.getInputStream("img1.jpg");
Bitmap bmp = BitmapFactory.decodeStream(iStream, null, options);// options to decode
This works perfectly. Now, how does one read an encrypted zip file created using the following:
zip -n -encrypt .jpg main.123.in.example.app.obb *.jpg
Is there any way we can read such an encrypted zip file? Or do we resort to the jobb tool to do the encryption and use StorageManager to mount encrypted obb and read from there?

open assets or raw file as File

Is there a way to open assets or raw resource as File ? I have method that needs open file as File but since i want store my file in assets or in raw folder i cannot access them.
I tried open it with file:///android_asset/filename.xml but received FileNotFound exception.
File file = new File("....");
x.load(file);
Is there a way to open assets or raw resource as File ?
No, sorry. They do not exist as files. You can only get an InputStream.
If you're willing to zip up the file first, you can do this:
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.texfilezip), dir, true);
File textFile = new File(dir, "textfile.txt");

Accessing file from android data directory

i want to access the /data/local/tmp for any random access file
(RAF) file how to get the path of that file?
go this way and read the raf file line by line but be assure that the folder have read permission to other user or you can change it from adb shell by using chmod commmand .....
File finalFile = new File("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
For accessing the any file through code , you must know the path of the file.
and then you can use the code:
File finalFile = new File/t("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
"/data/local/tmp" is the path for the file. It can differ file to file. So you must know the path of file before accessing it.

Categories

Resources