| package com.secondworld.univeralsdk; | 
|   | 
| import android.app.Activity; | 
| import android.content.Context; | 
| import android.content.SharedPreferences; | 
| import android.content.res.AssetManager; | 
|   | 
| import java.io.File; | 
| import java.io.FileOutputStream; | 
| import java.io.IOException; | 
| import java.io.InputStream; | 
| import java.io.OutputStreamWriter; | 
| import java.io.Writer; | 
| import java.util.HashMap; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * Created by Administrator on 2018/6/14 0014. | 
|  */ | 
|   | 
| public class FileUtil | 
| { | 
|   | 
|     private static final String TAG = "H2Engine_FileUtil"; | 
|   | 
|     /** | 
|      * Unity随包资源(StreamingAssets)拷贝至 persistentDataPath 路径下 | 
|      */ | 
|     public static void copyAssets(final Activity activity) | 
|     { | 
|   | 
|         // 判断是否已经执行过 | 
|         SharedPreferences _sp = activity.getPreferences(Context.MODE_PRIVATE); | 
|         Boolean _isCopied = hasCopy(activity); | 
|   | 
|         LogUtil.i(TAG, "是否已经执行过拷贝逻辑: " + _isCopied); | 
|   | 
|         // 获取当前包的版本号 | 
|         final String _currentVer = UniversalUtil.GetVersionName(activity); | 
|   | 
|         // 已经拷贝过, 则再次判断一次版本号 | 
|         // 如果版本号不相同, 则重新进行拷贝 | 
|         // 获取本地存储的版本号 | 
|         String _installVer = _sp.getString(StaticDefine.LS_KEY_VERSION, ""); | 
|   | 
|         if (!_installVer.equals(_currentVer)) | 
|         { | 
|             LogUtil.i(TAG, "版本比较过后发现不同: " + _installVer + " != " + _currentVer); | 
|             SharedPreferences.Editor _editor = _sp.edit(); | 
|             // 存储此次的版本信息 | 
|             _editor.putString(StaticDefine.LS_KEY_VERSION, _currentVer); | 
|             _editor.apply(); | 
|   | 
|             if (_isCopied) | 
|             { | 
|                 // 将本地存储修改为未拷贝过 | 
|                 deleteRecord(activity); | 
|                 _isCopied = false; | 
|             } | 
|         } | 
|   | 
|         if (_isCopied) | 
|         { | 
|             Map<String, Object> _msg = new HashMap<>(); | 
|             _msg.put("code", CodeA2U.AssetCopyFinished); | 
|             UniversalUtil.sendMessageToUnity(_msg); | 
|             return; | 
|         } | 
|   | 
|         new Thread(new Runnable() | 
|         { | 
|             @Override | 
|             public void run() | 
|             { | 
|   | 
|                 String _dest = activity.getExternalFilesDir("").getAbsolutePath(); | 
|                 AssetManager _assetMgr = activity.getAssets(); | 
|   | 
|                 try | 
|                 { | 
|   | 
|                     String[] _fileNames = _assetMgr.list("android"); | 
|                     if (_fileNames != null) | 
|                     { | 
|                         for (String _fileName : _fileNames) | 
|                         { | 
|                             copy(activity, "android" + File.separator + _fileName, | 
|                                  _dest + File.separator + _fileName); | 
|                         } | 
|                     } | 
|   | 
|                     recordCopy(activity); | 
|   | 
|                     Map<String, Object> _msg = new HashMap<>(); | 
|                     _msg.put("code", CodeA2U.AssetCopyFinished); | 
|                     UniversalUtil.sendMessageToUnity(_msg); | 
|   | 
|                 } catch (Exception e) | 
|                 { | 
|                     e.printStackTrace(); | 
|                 } | 
|   | 
|             } | 
|         }).start(); | 
|     } | 
|   | 
|     public static void copy(Context context, String fileName) | 
|     { | 
|         String _originalPath = "android" + File.separator + fileName; | 
|         String _destPath = context.getExternalFilesDir( | 
|                 "").getAbsolutePath() + File.separator + fileName; | 
|         String _destDir = _destPath.substring(0, _destPath.lastIndexOf('/') + 1); | 
|         File _file = new File(_destDir); | 
|         if(!_file.exists()) | 
|         { | 
|             LogUtil.i(TAG,"单独拷贝 => 不存在指定路径: " + _destDir + ", 这里创建..."); | 
|             _file.mkdir(); | 
|         } | 
|         try | 
|         { | 
|             InputStream _is = context.getAssets().open(_originalPath); | 
|             FileOutputStream _fos = new FileOutputStream(new File(_destPath)); | 
|             byte[] _buffer = new byte[1024]; | 
|             int _byteCount; | 
|             while ((_byteCount = _is.read(_buffer)) != -1) | 
|             { | 
|                 _fos.write(_buffer, 0, _byteCount); | 
|             } | 
|             _fos.flush(); | 
|             _is.close(); | 
|             _fos.close(); | 
|             LogUtil.i("FileUtil", "单独拷贝 => 文件: " + _originalPath + " 已拷贝至: " + _destPath); | 
|         } catch (Exception e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     public static void copy(Context context, String original, String dest) | 
|     { | 
|         try | 
|         { | 
|             String _fileNames[] = context.getAssets().list(original); | 
|             if (_fileNames.length > 0) | 
|             { | 
|                 File _dir = new File(dest); | 
|                 _dir.mkdir(); | 
|                 LogUtil.i("FileUtil", "[" + original + "] 是一个文件夹, 创建文件夹: [" + dest + "]"); | 
|                 for (String _fileName : _fileNames) | 
|                 { | 
|                     copy(context, original + File.separator + _fileName, | 
|                          dest + File.separator + _fileName); | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 InputStream _is = context.getAssets().open(original); | 
|                 FileOutputStream _fos = new FileOutputStream(new File(dest)); | 
|                 byte[] _buffer = new byte[1024]; | 
|                 int _byteCount; | 
|                 while ((_byteCount = _is.read(_buffer)) != -1) | 
|                 { | 
|                     _fos.write(_buffer, 0, _byteCount); | 
|                 } | 
|                 _fos.flush(); | 
|                 _is.close(); | 
|                 _fos.close(); | 
|                 LogUtil.i("FileUtil", "文件: " + original + " 已拷贝至: " + dest); | 
|             } | 
|         } catch (Exception e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     public static String getFileNameWithoutExtension(String fileName) | 
|     { | 
|         return fileName.substring(0, fileName.lastIndexOf(".")); | 
|     } | 
|   | 
|     private static boolean hasCopy(Context context) | 
|     { | 
|         File _file = new File(context.getExternalFilesDir(""), "/assetCopyFinish.txt"); | 
|         return _file.exists(); | 
|     } | 
|   | 
|     private static void deleteRecord(Context context) | 
|     { | 
|         File _file = new File(context.getExternalFilesDir(""), "/assetCopyFinish.txt"); | 
|         if (_file.exists()) | 
|         { | 
|             _file.delete(); | 
|         } | 
|     } | 
|   | 
|     private static void recordCopy(Context context) | 
|     { | 
|         File _file = new File(context.getExternalFilesDir(""), "/assetCopyFinish.txt"); | 
|         try | 
|         { | 
|             FileOutputStream _fos = new FileOutputStream(_file); | 
|             Writer _writer = new OutputStreamWriter(_fos, "UTF-8"); | 
|             _writer.write(1); | 
|             _writer.close(); | 
|             _fos.close(); | 
|         } catch (IOException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
| } |