美文网首页
安卓导入web html 文件快速读写方法

安卓导入web html 文件快速读写方法

作者: 陈陈_04d0 | 来源:发表于2020-05-29 09:46 被阅读0次

总所周知在安卓的资源文件中可以直接放入文件,webView 加载的时候可以直接调用(webview.loadUrl("file:///android_asset/"+文件名称)),

但是assets 文件不支持写入文件,如果我们要更新html 文件怎么办呢。

1、我们可以把初始版本html 文件压缩到assets 下,然后在解压到本地文件中去。如果后面有小的更新我们只需要根据文件MD5去更新

2、如果是大的更新我们只需要重新导入压缩包到assets 下面打包即可。特别注意assets 文件压缩直接最好检查一下是否包含中文,不然会解压不成功

/**/**

* 解压缩功能.

* 将zipFile文件解压到folderPath目录下.

* ZipFile 解压全部文件效率高于 ZipInputStream

* @throws Exception

*/

public static int upZipFile(File zipFile, String folderPath, ISiteFileManagetEvent event)throws IOException {

ZipFile zfile =new ZipFile(zipFile);

    Enumeration zList = zfile.entries();

    ZipEntry ze =null;

    byte[] buf =new byte[1024];

    int nowLength =0;

    while (zList.hasMoreElements()) {

ze = (ZipEntry) zList.nextElement();

        if (ze.isDirectory()) {

Log.e("upZipFile", ze.getName());

            String dirstr = folderPath + ze.getName();

            dirstr =new String(dirstr.getBytes("8859_1"), "GB2312");

            File f =new File(dirstr);

            f.mkdir();

continue;

        }

OutputStream os =new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));

        InputStream is =new BufferedInputStream(zfile.getInputStream(ze));

        int readLen =0;

        while ((readLen = is.read(buf, 0, 1024)) != -1) {

os.write(buf, 0, readLen);

        }

nowLength +=1;

        event.SiteFileManagetProcess(nowLength, 201);

        is.close();

        os.close();

    }

event.SiteFileManagetFinishEvent("");

    if (zipFile !=null) {

boolean delete = zipFile.delete();

        Log.e("BaseFileUtil1", delete +"");

    }

zfile.close();

    return 0;

}

/**

* 给定根目录,返回一个相对路径所对应的实际文件名.

*

* @param baseDir    指定根目录

* @param absFileName 相对路径名,来自于ZipEntry中的name

* @return java.io.File 实际的文件

*/

public static FilegetRealFileName(String baseDir, String absFileName) {

String[] dirs = absFileName.split("/");

    File ret =new File(baseDir);

    String substr =null;

    if (dirs.length >1) {

for (int i =0; i < dirs.length -1; i++) {

substr = dirs[i];

            try {

//substr.trim();

                substr =new String(substr.getBytes("8859_1"), "GB2312");

            }catch (UnsupportedEncodingException e) {

e.printStackTrace();

            }

ret =new File(ret, substr);

        }

if (!ret.exists())

ret.mkdirs();

        substr = dirs[dirs.length -1];

        try {

substr =new String(substr.getBytes("8859_1"), "GB2312");

        }catch (UnsupportedEncodingException e) {

e.printStackTrace();

        }

ret =new File(ret, substr);

        return ret;

    }

return ret;

}

* 复制zip 压缩文件到 本地

* @param strOutFileName 本地文件路径

* @param zipName        assets 文件名称需要带文件后缀 xx.zip

* @throws IOException

*/

public static void copyBigDataToSD(Context context, String strOutFileName, String zipName)throws IOException {

InputStream myInput;

    OutputStream myOutput =new FileOutputStream(strOutFileName);

    //操作assets 文件

    myInput = context.getAssets().open(zipName);

    byte[] buffer =new byte[1024];

    int length = myInput.read(buffer);

    while (length >0) {

myOutput.write(buffer, 0, length);

        length = myInput.read(buffer);

    }

myOutput.flush();

    myInput.close();

    myOutput.close();

}

相关文章

网友评论

      本文标题:安卓导入web html 文件快速读写方法

      本文链接:https://www.haomeiwen.com/subject/aqztzhtx.html