Android复制Assets中大于1M的Zip压缩包,并解压到SD卡中
1 package com.demo.asset; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.util.Enumeration; 9 import java.util.zip.ZipEntry; 10 import java.util.zip.ZipException; 11 import java.util.zip.ZipFile; 12 13 import android.app.Activity; 14 import android.os.Bundle; 15 import android.os.Environment; 16 17 public class MainActivity extends Activity { 18 /** Called when the activity is first created. */ 19 20 private String ASSETS_NAME = "SETTING.zip"; 21 private String DB_PATH = Environment.getExternalStorageDirectory() + "/word/rar/"; 22 private String DB_TOPATH = Environment.getExternalStorageDirectory() + "/word/db/"; 23 private String DB_NAME = "SETTING.zip"; 24 25 @Override 26 public void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.main); 29 30 //拷贝文件 31 try { 32 copyDataBase(); 33 } catch (IOException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 38 //解压文件 39 String path= DB_PATH + DB_NAME; 40 File zipFile= new File(path); 41 try { 42 upZipFile(zipFile, DB_TOPATH); 43 } catch (ZipException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 51 } 52 53 private void copyDataBase() throws IOException { 54 // Path to the just created empty db 55 String outFileName = DB_PATH + DB_NAME; 56 // 判断目录是否存在。如不存在则创建一个目录 57 File file = new File(DB_PATH); 58 if (!file.exists()) { 59 file.mkdirs(); 60 } 61 file = new File(outFileName); 62 if (!file.exists()) { 63 file.createNewFile(); 64 } 65 // Open your local db as the input stream 66 InputStream myInput = this.getAssets().open(ASSETS_NAME); 67 // Open the empty db as the output stream128 68 OutputStream myOutput = new FileOutputStream(outFileName); 69 // transfer bytes from the inputfile to the outputfile130 70 byte[] buffer = new byte[1024]; 71 int length; 72 while ((length = myInput.read(buffer)) > 0) { 73 myOutput.write(buffer, 0, length); 74 } 75 // Close the streams136 76 myOutput.flush(); 77 myOutput.close(); 78 myInput.close(); 79 } 80 81 /** 82 * 解压缩一个文件 83 * 84 * @param zipFile 85 * 要解压的压缩文件 86 * @param folderPath 87 * 解压缩的目标目录 88 * @throws IOException 89 * 当解压缩过程出错时抛出 90 */ 91 public void upZipFile(File zipFile, String folderPath) throws ZipException, IOException { 92 File desDir = new File(folderPath); 93 if (!desDir.exists()) { 94 desDir.mkdirs(); 95 } 96 97 ZipFile zf = new ZipFile(zipFile); 98 for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) { 99 ZipEntry entry = ((ZipEntry) entries.nextElement()); 100 InputStream in = zf.getInputStream(entry); 101 String str = folderPath + File.separator + entry.getName(); 102 str = new String(str.getBytes("8859_1"), "GB2312"); 103 File desFile = new File(str); 104 if (!desFile.exists()) { 105 File fileParentDir = desFile.getParentFile(); 106 if (!fileParentDir.exists()) { 107 fileParentDir.mkdirs(); 108 } 109 desFile.createNewFile(); 110 } 111 OutputStream out = new FileOutputStream(desFile); 112 byte buffer[] = new byte[1024]; 113 int realLength; 114 while ((realLength = in.read(buffer)) > 0) { 115 out.write(buffer, 0, realLength); 116 } 117 in.close(); 118 out.close(); 119 } 120 } 121 122 }
posted on 2012-04-24 12:44 simpleceo 阅读(1860) 评论(0) 编辑 收藏 举报