@@ -341,12 +341,16 @@ public static void unZipFile(String archive, String decompressDir, Promise promi
341341
342342
343343 @ ReactMethod
344- public static void deleteFile (String zippath , Promise promise ) {
344+ public static void deleteFile (String path , Promise promise ) {
345345 try {
346- File file = new File (zippath );
346+ File file = new File (path );
347347 boolean result = false ;
348348 if (file .exists ()) {
349- result = file .delete ();
349+ if (file .isDirectory ()) {
350+ result = deleteDirectory (path );
351+ } else {
352+ result = deleteFile (path );
353+ }
350354 }
351355 promise .resolve (result );
352356 }catch (Exception e ){
@@ -535,21 +539,27 @@ public static Boolean initUserDefaultData(String userName, Context context) {
535539 userName = userName == null || userName .equals ("" ) ? "Customer" : userName ;
536540
537541 // 初始化用户工作空间
538- String downloadsPath = SDCARD + "/iTablet/User/" + userName + "/ExternalData/" ;
539- String dataPath = SDCARD + "/iTablet/User/" + userName + "/Data/" ;
540- // String dataPath2 = SDCARD + "/iTablet/User/" + userName + "/Data/Scene/";
541- String originName = "Customer.smwu" ;
542- // String originName2 = "OlympicGreen_android.zip";
543- String wsName = "Workspace.smwu" ;
544- // String ssName = "OlympicGreen_android.zip";
545- if (!Utils .fileIsExit (dataPath + wsName )) {
546- Utils .copyAssetFileToSDcard (context .getApplicationContext (), dataPath , originName , wsName );
542+ String userPath = SDCARD + "/iTablet/User/" + userName + "/" ;
543+ String downloadsPath = userPath + "ExternalData/" ;
544+ String dataPath = userPath + "Data/" ;
545+
546+ String defaultData = "DefaultData" ;
547+ String defaultDataPath = SDCARD + "/iTablet/User/" + userName + "/" + defaultData + "/" ;
548+ // String originName = "Customer.smwu";
549+ String originName = "Workspace.zip" ;
550+ String defaultDataZip = "DefaultData.zip" ;
551+ String wsName = "Workspace.sxwu" ;
552+
553+ if (!Utils .fileIsExit (defaultDataPath + wsName )) {
554+ Utils .copyAssetFileToSDcard (context .getApplicationContext (), userPath , originName , defaultDataZip );
555+ if (Utils .fileIsExit (userPath + defaultDataZip )) {
556+ FileTools .unZipFile (userPath + defaultDataZip , defaultDataPath );
557+ }
558+ }
559+ File defaultDataFile = new File (userPath + defaultDataZip );
560+ if (defaultDataFile .exists ()) {
561+ defaultDataFile .delete ();
547562 }
548- // if (!Utils.fileIsExit(dataPath2 + ssName)) {
549- // Utils.copyAssetFileToSDcard(context.getApplicationContext(), dataPath2, originName2, ssName);
550- // Decompressor.UnZipFolder(dataPath2 + originName2, dataPath2);
551- // Utils.deleteFile(dataPath2 + originName2);
552- // }
553563
554564 //创建用户目录
555565 createDirectory (dataPath + "Attribute" );
@@ -682,5 +692,58 @@ public static boolean isMessyCode(String strName) {
682692 return false ;
683693
684694 }
695+
696+
697+
698+ public static boolean deleteFile (String path ) {
699+ File file = new File (path );
700+ boolean result = false ;
701+ if (file .exists ()) {
702+ if (!file .isDirectory ())
703+ result = file .delete ();
704+ }
705+ return result ;
706+ }
707+
708+ public static boolean deleteDirectory (String path ) {
709+ // 如果path不以文件分隔符结尾,自动添加文件分隔符
710+ if (!path .endsWith (File .separator ))
711+ path = path + File .separator ;
712+ File pathFile = new File (path );
713+ // 如果path对应的文件不存在,或者不是一个目录,则退出
714+ if ((!pathFile .exists ()) || (!pathFile .isDirectory ())) {
715+ System .out .println ("删除目录失败:" + path + "不存在!" );
716+ return false ;
717+ }
718+ boolean flag = true ;
719+ // 删除文件夹中的所有文件包括子目录
720+ File [] files = pathFile .listFiles ();
721+ for (int i = 0 ; i < files .length ; i ++) {
722+ // 删除子文件
723+ if (files [i ].isFile ()) {
724+ flag = deleteFile (files [i ].getAbsolutePath ());
725+ if (!flag )
726+ break ;
727+ }
728+ // 删除子目录
729+ else if (files [i ].isDirectory ()) {
730+ flag = deleteDirectory (files [i ]
731+ .getAbsolutePath ());
732+ if (!flag )
733+ break ;
734+ }
735+ }
736+ if (!flag ) {
737+ System .out .println ("删除目录失败!" );
738+ return false ;
739+ }
740+ // 删除当前目录
741+ if (pathFile .delete ()) {
742+ System .out .println ("删除目录" + path + "成功!" );
743+ return true ;
744+ } else {
745+ return false ;
746+ }
747+ }
685748}
686749
0 commit comments