我需要一个小代码片段,它从给定的.zip文件中解压缩一些文件,并根据它们在压缩文件中的格式提供单独的文件.请发布您的知识并帮助我.
在Android 4.4(KitKat)上,谷歌已经限制了对SD卡的访问.
从Android Lollipop(5.0)开始,开发人员可以使用新的API,要求用户确认允许访问特定文件夹,如此Google-Groups帖子所述.
该帖子指导您访问两个网站:
这看起来像是一个内部示例(可能稍后会在API演示中显示),但很难理解发生了什么.
这是新API的官方文档,但它没有详细说明如何使用它.
这是它告诉你的:
如果您确实需要完全访问整个文档子树,请首先启动ACTION_OPEN_DOCUMENT_TREE以允许用户选择目录.然后将生成的getData()传递给fromTreeUri(Context,Uri)以开始使用用户选择的树.
在导航DocumentFile实例树时,您始终可以使用getUri()来获取表示该对象的基础文档的Uri,以便与openInputStream(Uri)等一起使用.
要在运行KITKAT或更早版本的设备上简化代码,可以使用fromFile(File)来模拟DocumentsProvider的行为.
我对新API有几个问题:
我从网上获得了以下代码,它看起来一切正常,但我发现文件未找到异常...
我在sdcard中有一个名为NewForestPonies.epub的文件
许可:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
码:
String ZipFileLocation=Environment.getExternalStorageDirectory()+"/NewForestPonies.epub";
String unZipFileLocation=Environment.getExternalStorageDirectory()+"/DEST/";
Decompress decomp=new Decompress(ZipFileLocation, unZipFileLocation, "zip");
decomp.run();
public Decompress(String zipFile, String location,String t) {
super(t);
_zipFile = zipFile;
_location = location;
}
public void run() {
FileInputStream fin=null;
ZipInputStream zin=null;
File file =null;
ZipEntry ze ;
FileOutputStream fout=null;
try{
System.out.println(_zipFile );
System.out.println(_location);
fin = new FileInputStream(_zipFile);
zin = new ZipInputStream(fin);
ze= null;
byte[] buffer = new byte[1024];
int length;
while ((ze = zin.getNextEntry()) != null) {
file = …Run Code Online (Sandbox Code Playgroud) 要解压缩zip文件,我已经使用了包java.util.zip*中的类,通过引用它并且它正常工作但是解压缩40MB的文件需要59秒.当我在iPhone项目上尝试相同的zip文件时(我们正在为两个平台开发应用程序--Android和iPone以及解压缩zip文件并将解压缩的内容保存到SDCARD-Android或文档目录 - iPhone),只需14秒.iPhone应用程序使用ziparchive.
所以我的问题是:
1.从上面的实验中可以看出,与iPhone应用程序相比,Java中对SDCARD的解压缩和文件写入操作消耗的时间更长,因此我决定使用NDK进行C/C++级解压缩和文件写入操作.这是正确的选择吗?
2.我搜索了谷歌,stackoverflow和一些建议使用minizip但没有足够的帮助如何在Android中使用minizip.anyboday已经尝试过minizip for android吗?
3.我也尝试了用于libz的NDK开发来实现我的目标,因为Libz被添加到NDK但没有得到如何使用它.在NDK有人试过libz吗?
4.在Java或C/C++中是否有其他任何框架解压缩大型zip文件并在更短的时间内将它们写入SDCARD?
请帮我.
这是我的Java解压缩代码:
public String unzip() {
String result;
try {
FileInputStream fin = new FileInputStream(this.filePath);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Unzip", "Unzipping " + ze.getName());
if (ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
// Read 16 k at a time
byte[] buffer = new byte[16*1024];
int read;
FileOutputStream fout = …Run Code Online (Sandbox Code Playgroud) 我试图解压缩压缩文件(此文件包含许多子文件夹和文件).
I am not able to create sub-folders while unzipping the file.
Run Code Online (Sandbox Code Playgroud)
每次我收到错误说:
No such file or directory.
Run Code Online (Sandbox Code Playgroud)
我搜索了很多关于这个的信息:
但是,没有任何帮助我.
以下是我的尝试:
public class UnZipper {
private static final String TAG = "UnZip";
private String mFileName, mDestinationPath;
public UnZipper(String fileName, String destinationPath) {
mFileName = fileName;
mDestinationPath = destinationPath;
}
public String getFileName() {
return mFileName;
}
public String getDestinationPath() {
return mDestinationPath;
}
// shrikant
public void unzip() {
String fullPath = mFileName;
Log.d(TAG, …Run Code Online (Sandbox Code Playgroud)