/** 判断SD卡是否挂载 */
    public static boolean isSDCardAvailable() {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return true;
        } else {
            return false;
        }
    }
    public static final String ROOT_DIR = "emar";
    /** 获取SD下的应用目录 */
    public static String getExternalStoragePath() {
        StringBuilder sb = new StringBuilder();
        sb.append(Environment.getExternalStorageDirectory().getAbsolutePath());
        sb.append(File.separator);
        sb.append(ROOT_DIR);
        sb.append(File.separator);
        return sb.toString();
    }

    /** 获取应用的cache目录 */
    public static String getCachePath(Context context) {
        File f = context.getCacheDir();
        if (null == f) {
            return null;
        } else {
            return f.getAbsolutePath() + "/";
        }
    }
    /**
     * 创建文件
     * @param filePath
     * @return
     */
    public static File createFile(String filePath) {
        File file = new File(filePath);
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        return file;
    }
    public static void copyBigDataToSD(Context context, String strOutFileName, String zipName) throws IOException  {  
        InputStream myInput;  
        OutputStream myOutput = new FileOutputStream(strOutFileName);  
        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();        
    }
   /**
     * 解压缩功能.
     * 将zipFile文件解压到folderPath目录下.
     * @throws Exception
     */
     public static int upZipFile(File zipFile, String folderPath) throws ZipException,IOException {
         ZipFile zfile = new ZipFile(zipFile);
         Enumeration<?> zList = zfile.entries();
         ZipEntry ze=null;
         byte[] buf=new byte[1024];
         while(zList.hasMoreElements()){
             ze=(ZipEntry)zList.nextElement();    
             if(ze.isDirectory()){
                 String dirstr = folderPath + ze.getName();
                 //dirstr.trim();
                 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);
             }
             is.close();
             os.close();    
         }
         zfile.close();
         return 0;
     }
     
     /**
      * 给定根目录,返回一个相对路径所对应的实际文件名.
      * @param baseDir 指定根目录
      * @param absFileName 相对路径名,来自于ZipEntry中的name
      * @return java.io.File 实际的文件
      */
      public static File getRealFileName(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.trim();
                  substr = new String(substr.getBytes("8859_1"), "GB2312");
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              }
              
              ret=new File(ret, substr);
              return ret;
          }
          return ret;
      }
// 把assets中的h5.zip拷贝到sd上,并解压此zip
String baseDir = FileUtils.isSDCardAvailable() ? FileUtils.getExternalStoragePath() : FileUtils.getCachePath(this);
String locationZip = baseDir + IntentTAG.LOCATION_H5_BASE_PATH + IntentTAG.LOCATION_H5_ZIP_NAME;
try {
    BaseFileUtil.createFile(locationZip);
    BaseFileUtil.copyBigDataToSD(this, locationZip, IntentTAG.LOCATION_H5_ZIP_NAME);
    BaseFileUtil.upZipFile(new File(locationZip), baseDir + IntentTAG.LOCATION_H5_BASE_PATH);
} catch (Exception e) {
    e.printStackTrace();
    BaseFileUtil.delFile(baseDir + IntentTAG.LOCATION_H5_BASE_PATH); // 解压失败,删除此文件夹下所有文件
}

 

posted on 2015-12-15 13:43  AK-Star  阅读(1299)  评论(1)    收藏  举报