- 浏览: 45828 次
- 性别:
- 来自: 上海
文章分类
最新评论
转载:http://blog.csdn.net/hopehe888999/article/details/19035373
package com.johnny.testzipanddownload; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.AsyncTask; import android.util.Log; public class DownLoaderTask extends AsyncTask<Void, Integer, Long> { private final String TAG = "DownLoaderTask"; private URL mUrl; private File mFile; private ProgressDialog mDialog; private int mProgress = 0; private ProgressReportingOutputStream mOutputStream; private Context mContext; public DownLoaderTask(String url,String out,Context context){ super(); if(context!=null){ mDialog = new ProgressDialog(context); mContext = context; } else{ mDialog = null; } try { mUrl = new URL(url); String fileName = new File(mUrl.getFile()).getName(); mFile = new File(out, fileName); Log.d(TAG, "out="+out+", name="+fileName+",mUrl.getFile()="+mUrl.getFile()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void onPreExecute() { // TODO Auto-generated method stub //super.onPreExecute(); if(mDialog!=null){ mDialog.setTitle("Downloading..."); mDialog.setMessage(mFile.getName()); mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mDialog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub cancel(true); } }); mDialog.show(); } } @Override protected Long doInBackground(Void... params) { // TODO Auto-generated method stub return download(); } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub //super.onProgressUpdate(values); if(mDialog==null) return; if(values.length>1){ int contentLength = values[1]; if(contentLength==-1){ mDialog.setIndeterminate(true); } else{ mDialog.setMax(contentLength); } } else{ mDialog.setProgress(values[0].intValue()); } } @Override protected void onPostExecute(Long result) { // TODO Auto-generated method stub //super.onPostExecute(result); if(mDialog!=null&&mDialog.isShowing()){ mDialog.dismiss(); } if(isCancelled()) return; ((MainActivity)mContext).showUnzipDialog(); } private long download(){ URLConnection connection = null; int bytesCopied = 0; try { connection = mUrl.openConnection(); int length = connection.getContentLength(); if(mFile.exists()&&length == mFile.length()){ Log.d(TAG, "file "+mFile.getName()+" already exits!!"); return 0l; } mOutputStream = new ProgressReportingOutputStream(mFile); publishProgress(0,length); bytesCopied =copy(connection.getInputStream(),mOutputStream); if(bytesCopied!=length&&length!=-1){ Log.e(TAG, "Download incomplete bytesCopied="+bytesCopied+", length"+length); } mOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bytesCopied; } private int copy(InputStream input, OutputStream output){ byte[] buffer = new byte[1024*8]; BufferedInputStream in = new BufferedInputStream(input, 1024*8); BufferedOutputStream out = new BufferedOutputStream(output, 1024*8); int count =0,n=0; try { while((n=in.read(buffer, 0, 1024*8))!=-1){ out.write(buffer, 0, n); count+=n; } out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } private final class ProgressReportingOutputStream extends FileOutputStream{ public ProgressReportingOutputStream(File file) throws FileNotFoundException { super(file); // TODO Auto-generated constructor stub } @Override public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException { // TODO Auto-generated method stub super.write(buffer, byteOffset, byteCount); mProgress += byteCount; publishProgress(mProgress); } } }
package com.johnny.testzipanddownload; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.AsyncTask; import android.util.Log; public class ZipExtractorTask extends AsyncTask<Void, Integer, Long> { private final String TAG = "ZipExtractorTask"; private final File mInput; private final File mOutput; private final ProgressDialog mDialog; private int mProgress = 0; private final Context mContext; private boolean mReplaceAll; public ZipExtractorTask(String in, String out, Context context, boolean replaceAll){ super(); mInput = new File(in); mOutput = new File(out); if(!mOutput.exists()){ if(!mOutput.mkdirs()){ Log.e(TAG, "Failed to make directories:"+mOutput.getAbsolutePath()); } } if(context!=null){ mDialog = new ProgressDialog(context); } else{ mDialog = null; } mContext = context; mReplaceAll = replaceAll; } @Override protected Long doInBackground(Void... params) { // TODO Auto-generated method stub return unzip(); } @Override protected void onPostExecute(Long result) { // TODO Auto-generated method stub //super.onPostExecute(result); if(mDialog!=null&&mDialog.isShowing()){ mDialog.dismiss(); } if(isCancelled()) return; } @Override protected void onPreExecute() { // TODO Auto-generated method stub //super.onPreExecute(); if(mDialog!=null){ mDialog.setTitle("Extracting"); mDialog.setMessage(mInput.getName()); mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mDialog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub cancel(true); } }); mDialog.show(); } } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub //super.onProgressUpdate(values); if(mDialog==null) return; if(values.length>1){ int max=values[1]; mDialog.setMax(max); } else mDialog.setProgress(values[0].intValue()); } private long unzip(){ long extractedSize = 0L; Enumeration<ZipEntry> entries; ZipFile zip = null; try { zip = new ZipFile(mInput); long uncompressedSize = getOriginalSize(zip); publishProgress(0, (int) uncompressedSize); entries = (Enumeration<ZipEntry>) zip.entries(); while(entries.hasMoreElements()){ ZipEntry entry = entries.nextElement(); if(entry.isDirectory()){ continue; } File destination = new File(mOutput, entry.getName()); if(!destination.getParentFile().exists()){ Log.e(TAG, "make="+destination.getParentFile().getAbsolutePath()); destination.getParentFile().mkdirs(); } if(destination.exists()&&mContext!=null&&!mReplaceAll){ } ProgressReportingOutputStream outStream = new ProgressReportingOutputStream(destination); extractedSize+=copy(zip.getInputStream(entry),outStream); outStream.close(); } } catch (ZipException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { zip.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return extractedSize; } private long getOriginalSize(ZipFile file){ Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) file.entries(); long originalSize = 0l; while(entries.hasMoreElements()){ ZipEntry entry = entries.nextElement(); if(entry.getSize()>=0){ originalSize+=entry.getSize(); } } return originalSize; } private int copy(InputStream input, OutputStream output){ byte[] buffer = new byte[1024*8]; BufferedInputStream in = new BufferedInputStream(input, 1024*8); BufferedOutputStream out = new BufferedOutputStream(output, 1024*8); int count =0,n=0; try { while((n=in.read(buffer, 0, 1024*8))!=-1){ out.write(buffer, 0, n); count+=n; } out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } private final class ProgressReportingOutputStream extends FileOutputStream{ public ProgressReportingOutputStream(File file) throws FileNotFoundException { super(file); // TODO Auto-generated constructor stub } @Override public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException { // TODO Auto-generated method stub super.write(buffer, byteOffset, byteCount); mProgress += byteCount; publishProgress(mProgress); } } }
package com.johnny.testzipanddownload; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { private final String TAG="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Environment.getExternalStorageDirectory()="+Environment.getExternalStorageDirectory()); Log.d(TAG, "getCacheDir().getAbsolutePath()="+getCacheDir().getAbsolutePath()); showDownLoadDialog(); //doZipExtractorWork(); //doDownLoadWork(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void showDownLoadDialog(){ new AlertDialog.Builder(this).setTitle("确认") .setMessage("是否下载?") .setPositiveButton("是", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.d(TAG, "onClick 1 = "+which); doDownLoadWork(); } }) .setNegativeButton("否", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.d(TAG, "onClick 2 = "+which); } }) .show(); } public void showUnzipDialog(){ new AlertDialog.Builder(this).setTitle("确认") .setMessage("是否解压?") .setPositiveButton("是", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.d(TAG, "onClick 1 = "+which); doZipExtractorWork(); } }) .setNegativeButton("否", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.d(TAG, "onClick 2 = "+which); } }) .show(); } public void doZipExtractorWork(){ //ZipExtractorTask task = new ZipExtractorTask("/storage/usb3/system.zip", "/storage/emulated/legacy/", this, true); ZipExtractorTask task = new ZipExtractorTask("/storage/emulated/legacy/testzip.zip", "/storage/emulated/legacy/", this, true); task.execute(); } private void doDownLoadWork(){ DownLoaderTask task = new DownLoaderTask("http://192.168.9.155/johnny/testzip.zip", "/storage/emulated/legacy/", this); //DownLoaderTask task = new DownLoaderTask("http://192.168.9.155/johnny/test.h264", getCacheDir().getAbsolutePath()+"/", this); task.execute(); } }
发表评论
-
android 判断ImageView当前显示的是哪一张图片
2015-12-29 14:40 1607判断ImageView当前显示的是哪一张图片 private ... -
Android 4.4及以上WebView问题
2015-10-27 17:41 10541. 4.4系统以上WebView页面内容重叠问题 连接 ... -
Android AlertDialog包含EditText,软键盘不能弹出的解决方法
2015-09-16 14:31 1060AlertDialog包含EditText,软键盘不能弹出的解 ... -
无法上传so文件到svn上
2015-05-25 16:48 872Window-->Team-->Ignored R ... -
Cocos2dx与Android进行交叉编译的大概步骤
2015-02-06 17:22 1125cocos2d交叉编译配置: 1.下载cygwin 64位ex ... -
博客链接
2014-11-26 16:01 01.好文章博客:http://blog.csdn.net/xi ... -
android一些基础功能汇总
2014-11-03 10:49 7281.Android开发 给图片加边框 http://www. ... -
创建文件夹和文件
2014-10-22 15:15 674String commonPath = Environment ... -
Android 线程的使用(传递多个参数)
2014-08-06 11:08 21061.Thread new Thread(new Runnabl ... -
cocos2dx 内存管理
2014-08-04 18:18 571cocos2dx 内存管理 转载自:http://blog. ... -
android读取图片
2014-07-25 11:56 992[size=large]一:读取res中的图片 //读取本地r ... -
Android ListView里设置默认Item的背景颜色
2014-07-23 11:51 816<?xml version="1.0" ... -
以某一点旋转(RotateAnimation)
2014-06-20 12:07 1750//自适应屏幕大小 webview.getSetting ... -
popWindow
2014-05-23 18:07 380popWindow -
自定义跑马灯
2014-05-23 16:46 554自定义跑马灯 -
WebView的使用总结
2014-05-05 12:09 648//自适应屏幕大小 webview.getSettings ... -
android 动态设置布局
2014-03-26 11:03 9351.动态设置RelativeLayout的布局 // 根据 ... -
android移动开发的很好的功能的网页
2014-03-19 18:15 633android经典DEMO http://blog.csdn ... -
android Uri获取真实路径转换成File的方法
2014-02-14 16:23 1996有的时候要将android uri如content://me ... -
android学习的进阶(从零开始,从初级到高级)
2014-02-08 12:08 2153轻松几步学Android开发 1. ...
相关推荐
Android解压缩文件。Android原生的解压缩文件,使用时提供保存的路径即可
android 压缩 解压缩 工具类,可以压缩文件夹,解压文件到指定文件夹,递归解压解压所有文件。
javaandroid可用的ziprar解压缩代码实现.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android平台的7z压缩与解压缩.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
解决android 自带zip解压缩中文乱码的问题,导入zip.jar即可使用(与系统提供的用法相同),大小仅24k
另外还可以进行常规的复制移动删除重命名等操作,压缩文件的时候只能压缩成zip格式,不支持rar格式,解压缩也是紧支持zip格式,源码有比较详细的注释,需要的朋友可以下载研究一下默认编码UTF-8编译版本2.3.3,
Android 在线下载压缩包并解压到指定目录.zip
Android语言实现 解、压缩文件 代码
找了很久的解决android(java也行)上zip、rar压缩文件解压缩的代码 ,在自己项目中试了很不错。
另外还可以进行常规的复制移动删除重命名等操作,压缩文件的时候只能压缩成zip格式,不支持rar格式,解压缩也是紧支持zip格式,源码有比较详细的注释,需要的朋友可以下载研究一下默认编码UTF-8编译版本2.3.3,
在Java中通过ZipFile解析Android apk 压缩文件,获取版本号等信息,获取的manifest文件格式无法识别为xml,导致无法解析,该jar文件用来处理乱码文件,apk压缩文件解压缩乱码问题
通过调用UnzipUtils下面的UnzipUtils.createFile(locationZip);UnzipUtils.copyBigDataToSD(MainActivity.this, locationZip, ZIP_NAME);UnzipUtils.upZipFile(new File(locationZip)...
(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,root ,zip压缩解.zip(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,root ,zip压缩解.zip(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,...
主要介绍了Android实现zip文件压缩及解压缩的方法,涉及Android针对文件的遍历及zip压缩与解压缩的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
过去,您必须使用 PC 下载大文件,解压缩并复制到手机。 现在,您可以直接在智能手机上执行整个过程。 Zarchiver Pro 是可以满足您所有要求的完美应用程序。 如果您定期使用 OBB 文件安装 Android 游戏,则 ...
需要解压安卓备份的img文件的来拿把,在Windows中查看img,支持XP,VISTA,WIN7,WIN8,更高.. Android安卓img镜像文件解压,安卓备份文件解压,安卓备份的img文件解压缩 很方便
这是一款基于安卓平台手机端的解压缩工具。 Winrar for Android 中文版 Winrar for Android 是来自 RARLAB 的官方版本,是 PC 端 winrar 的姐妹,支持创建RAR和ZIP压缩文件,能够轻松解压 RAR, RAR5, ZIP, TAR, GZ,...
android镜像system.new.dat解包解压缩sdat2img。 使用说明:需安装python环境,将system.new.dat,system.transfer.list和解压文件放在同一目录下,linux系统下,直接执行.py文件;windows下,直接双击执行.bat文件
在国内,更方便的做法是先下载离线包,然后解压缩到android sdk的安装目录下,重启Eclipse后就会自动识别出来。 文件名 文件大小 说明 其他下载地址 android-17_r01.zip 45.6M Android 4.2 官网 sysimg_armv7a-17_...