Android程序解压缩zip文件,并加载显示解压后的文件内容
2011-11-15 12:03
525 查看
刚做了个demo用于解压缩本地zip文件,并用webview显示其中的一个html文件,直接上代码,需要的朋友可以看看
其中我的abc.zip文件是放在sdcard中的,里面有2个文件,解压后生成一个abc文件夹,文件夹下是解压缩后的2个文件,我用一个webview直接指定加载了解压后的一个html文件,做的比较粗糙,省去了文件存在判断,扫描文件名、文件类型,main.xml文件也很简单,通过上面代码应该可以看出其中的控件,这里不再写xml布局文件了。
更多信息可参考下面文章:
加载html与js:
http://blog.csdn.net/com360/article/details/6618086
解压缩zip文件
http://www.oschina.net/code/snippet_4873_4142
public class ZipActivity extends Activity { private static final String TAG = "HelloXmlActivity"; private WebView mWebView; private static LinkedHashMap<String, String> widgetInfoMap = new LinkedHashMap<String, String>(); //http://blog.csdn.net/com360/article/details/6618086 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String zipfile = "/sdcard/abc.zip"; try { unzip(zipfile, "/sdcard/");//yangguangfu/wujiali/ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } mWebView=(WebView)findViewById(R.id.web); mWebView.loadUrl("file:///sdcard/abc/aaa.html");//此处加载解压后的html内容 } /* * 这个是解压ZIP格式文件的方法 * * @zipFileName:是传进来你要解压的文件路径,包括文件的名字; * * @outputDirectory:选择你要保存的路劲; * */ private void unzip(String zipFileName, String outputDirectory) throws Exception { ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName)); ZipEntry z; String name = ""; String extractedFile = ""; int counter = 0; while ((z = in.getNextEntry()) != null) { name = z.getName(); Log.d(TAG, "unzipping file: " + name); if (z.isDirectory()) { Log.d(TAG, name + "is a folder"); // get the folder name of the widget name = name.substring(0, name.length() - 1); File folder = new File(outputDirectory + File.separator + name); folder.mkdirs(); if (counter == 0) { extractedFile = folder.toString(); } counter++; Log.d(TAG, "mkdir " + outputDirectory + File.separator + name); } else { Log.d(TAG, name + "is a normal file"); File file = new File(outputDirectory + File.separator + name); file.createNewFile(); // get the output stream of the file FileOutputStream out = new FileOutputStream(file); int ch; byte[] buffer = new byte[1024]; // read (ch) bytes into buffer while ((ch = in.read(buffer)) != -1) { // write (ch) byte from buffer at the position 0 out.write(buffer, 0, ch); out.flush(); } out.close(); } } in.close(); } }
其中我的abc.zip文件是放在sdcard中的,里面有2个文件,解压后生成一个abc文件夹,文件夹下是解压缩后的2个文件,我用一个webview直接指定加载了解压后的一个html文件,做的比较粗糙,省去了文件存在判断,扫描文件名、文件类型,main.xml文件也很简单,通过上面代码应该可以看出其中的控件,这里不再写xml布局文件了。
更多信息可参考下面文章:
加载html与js:
http://blog.csdn.net/com360/article/details/6618086
解压缩zip文件
http://www.oschina.net/code/snippet_4873_4142
相关文章推荐
- Android无需解压直接读取Zip文件和文件内容
- 关于JQueryAjax文件加载显示文本内容
- 文件基本操作---小程序---显示目标文件内容
- android zip4j之--解压zip文件并实时显示解压进度
- 002_010 Python 处理字符串中的Zip文件 你的程序收到了一个字符串,内容是一个ZIP文件,需要读取zip文件的信息
- 自制程序 dump & binwrite 验证 用UE来查看文件二进制内容,显示不完全正确 的问题
- Java无需解压直接读取Zip文件和文件内容
- cordova程序加载pdf文件的2种方法(ios/android)
- Java小程序之哈夫曼树与文件压缩和解压缩(三)文件解压篇
- Android如何将text文件内容显示到插件中
- android progressBar 加载内容的时候显示
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码
- Android 开发中,如何将多个程序打包成一个apk文件,但是安装完成后显示多个程序?
- swing实现拖拽效果(将本地文件拖拽到程序的文本框中显示内容)
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件
- android-AlertDialog 加载xml文件同时显示图片
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码
- Android webview加载网页只想显示部分内容
- android 显示pdf文件内容
- Java无需解压直接读取Zip文件和文件内容