问题描述
你怎么能读取Android的GZIP文件位于资产(或资源/ RAW)文件夹?
我曾尝试以下code,但我流的大小始终为1。
GZIPInputStream FIN =新GZIPInputStream(mContext.getResources()openRawResource(R.raw.myfilegz)。);
INT大小= fIn.available();
由于某种原因,大小始终是1。但是如果。我不GZIP文件,它工作正常。
注意:采用Android 1.5
公共类ResLoader {
/ **
* @参数水库
* @throws IOException异常
* @throws FileNotFoundException异常
* @throws IOException异常
* /
静态无效unpackResources()抛出FileNotFoundException异常,IOException异常{
最终诠释BUFFER = 8192;
android.content.res.Resources T = TestingE3d.mContext.getResources();
InputStream的FIS = t.openRawResource(R.raw.resources);
如果(FIS == NULL)
返回;
ZipInputStream ZIN =新ZipInputStream(新的BufferedInputStream(FIS,
缓冲));
ZipEntry的进入;
而((进入= zin.getNextEntry())!= NULL){
诠释计数;
FileOutputStream中FOS = TestingE3d.mContext.openFileOutput(入口
.getName(),0);
的BufferedOutputStream DEST =新的BufferedOutputStream(FOS,BUFFER);
字节的数据[] =新的字节[BUFFER]
而((计数= zin.read(数据,0,缓冲液))!= - 1){
dest.write(数据,0,计数);
// Log.v(NOTAG,写作+数+到+ entry.getName());
}
dest.flush();
dest.close();
}
zin.close();
}
}
R.raw.resources是一个zip文件 - 这个类DECOM preSS在压缩到本地文件夹中的所有文件。我用这个的NDK。
您可以从NDK通过访问费尔:/数据/数据//文件/
包=包,其中ResLoader驻留文件名=文件是原始/ resources.zip之一
How can you read GZIP file in Android located in the "ASSETS" (or resources/raw) folder?
I have tried the following code, but my stream size is always 1.
GZIPInputStream fIn = new GZIPInputStream(mContext.getResources().openRawResource(R.raw.myfilegz));
int size = fIn.available();
for some reason the size is always 1. But if Idon't GZIP the file, it works fine.
NOTE:Using Android 1.5
public class ResLoader {
/**
* @param res
* @throws IOException
* @throws FileNotFoundException
* @throws IOException
*/
static void unpackResources() throws FileNotFoundException, IOException {
final int BUFFER = 8192;
android.content.res.Resources t = TestingE3d.mContext.getResources();
InputStream fis = t.openRawResource(R.raw.resources);
if (fis == null)
return;
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis,
BUFFER));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
int count;
FileOutputStream fos = TestingE3d.mContext.openFileOutput(entry
.getName(), 0);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
byte data[] = new byte[BUFFER];
while ((count = zin.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
// Log.v("NOTAG", "writing "+count + " to "+entry.getName());
}
dest.flush();
dest.close();
}
zin.close();
}
}
R.raw.resources is a zip file - this class will decompress all files in that zip to your local folder.I use this for NDK.
you can access your fils from ndk through:/data/data//files/
package = package where ResLoader residesfilename = one of files that is in raw/resources.zip
这篇关于安卓:读取资产文件夹中的GZIP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!