2010-09-13 57 views
2

是否有可能使用暫停/恢復功能?遞歸文件夾/目錄複製與AS3 /空氣

source.copyTo(destination);

如果您能儘早發送,這將是一件好事。

+1

你的意思是在AIR中,對吧? – Amarghosh 2010-09-13 08:42:43

+0

yes AIR.to將文件夾從一個位置複製到另一個位置..編輯問題 – Amitd 2010-09-13 08:47:52

回答

5

我發現這裏的一個解決方案CookBook from Adobe

private function copyInto(directoryToCopy:File, locationCopyingTo:File):void 
{ 
    var directory:Array = directoryToCopy.getDirectoryListing(); 

    for each (var f:File in directory) 
    { 
     if (f.isDirectory) 
      copyInto(f, locationCopyingTo.resolvePath(f.name)); 
     else 
      f.copyTo(locationCopyingTo.resolvePath(f.name), true); 
    } 
} 
+0

這不會複製空文件夾? – alxx 2011-03-09 17:32:20

+0

nope foreach不會執行空目錄,如果directory.length = 0,只需複製目錄複製到要複製的位置即。 directoryCopying.copyTo(locationCopyingTo.resolvePath(directoryToCopy.name),true); – Amitd 2011-03-10 18:58:24

1

或者你可以只使用File.copyTo()方法:

var source:File = new File(); 
source.resolvePath('sourceFolder'); 
var destination:File = new File(); 
destination.resolvePath('destinationFolder'); 
source.copyTo(destination); 

如果目錄很大,你不希望你的應用程序被卡等待複製時,可以使用copyToAsync,它會在作業完成時使源文件分派Event.COMPLETE。

0

這是上面修改的代碼,如果有人想複製整個目錄;空文件夾和所有。注意參數中要使用的「copyEmptyFolders」參數。

//Recursivley copies directory. 
    private static function copyInto(directoryToCopy:File, locationCopyingTo:File, copyEmptyFolders:Boolean=true):void 
    { 

     var directory:Array = directoryToCopy.getDirectoryListing(); 

     for each (var f:File in directory) 
     { 
      if (f.isDirectory) 
      { 

       // Copies a folder whether it is empty or not. 
       if(copyEmptyFolders) f.copyTo(locationCopyingTo.resolvePath(f.name), true); 

       // Recurse thru folder. 
       copyInto(f, locationCopyingTo.resolvePath(f.name)); 

      } 
      else 
       f.copyTo(locationCopyingTo.resolvePath(f.name), true); 

     } 

    }