问题
how to read from a zipfile in a folder which was selected by the ACTION_OPEN_DOCUMENT_TREE Intent ?
My app let the user choose a folder through the ACTION_OPEN_DOCUMENT_TREE Intent. In that folder i will have a Zipfile with a specific name. Goal is to read that Zipfile with java.util.zip.ZipFile.
How do I instantiate a ZipFile with this specific Zipfilename from the provided URI (Folderinfo) in onActivityResult ?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri treeUri = data.getData();
String sPath=treeUri.getPath();
java.util.zip.ZipFile myzip=new java.util.zip.ZipFile("mypath"); <-- Constructor expects File or Path as String. Howto handle this with the Uri ?
回答1:
How do I instantiate a ZipFile with this specific Zipfilename from the provided URI (Folderinfo) in onActivityResult ?
You can't, as there is no file, and ZipFile
needs a file. You can only get an InputStream
, using openInputStream()
on a ContentResolver
, and that only if you get a Uri
to that specific file that you are looking for.
Your options appear to be:
Use
ZipInputStream
, which can wrap anInputStream
Find some third-party library that accepts an
InputStream
as input and gives you a better APICopy the ZIP flie to your app's portion of internal storage and read its contents using
ZipFile
回答2:
I was working on this problem and ended up going with a strategy @CommonsWare mentioned as the third option, which is copying a file to non-sdcard place and load as a ZipFile. It works well so I share my snippet for everybody.
public static ZipFile loadCachedZipFromUri(Context context, Uri uri, String filename){
File file = new File(context.getCacheDir(), filename);
String fileName = context.getCacheDir().getAbsolutePath() + '/' + filename;
ZipFile zip = null;
if (file.exists()){
Log.d(TAG, "file exists");
try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // N for Nougat
zip = new ZipFile(fileName, Charset.forName("ISO-8859-1"));
}else{
zip = new ZipFile(fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return zip;
}
DocumentFile dest = DocumentFile.fromFile(file);
InputStream in = null;
OutputStream out = null;
Log.d(TAG, "Copy started");
try {
in = context.getContentResolver().openInputStream(uri);
out = context.getContentResolver().openOutputStream(dest.getUri());
byte[] buf = new byte[2048];
int len = 0;
while((len = in.read(buf)) >= 0){
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d(TAG, "Load copied file");
try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // N for Nougat
zip = new ZipFile(fileName, Charset.forName("ISO-8859-1"));
}else{
zip = new ZipFile(fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return zip;
}
来源:https://stackoverflow.com/questions/29746924/how-to-read-from-a-zipfile-in-a-folder-which-was-selected-by-the-action-open-doc