将文件夹从 iPhone 资源目录复制到文档目录

Posted

技术标签:

【中文标题】将文件夹从 iPhone 资源目录复制到文档目录【英文标题】:Copy folder from iPhone Resources directory to document directory 【发布时间】:2010-01-31 09:51:38 【问题描述】:
BOOL success;
NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                      NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"DB"];
success = [fileManager fileExistsAtPath:documentDBFolderPath];

if (success)
 return;
else
 NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
                                         stringByAppendingPathComponent:@"DB"];
 [fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath           
                                                                         error:&error];
 

像这样。

Resources/DB/words.csv => DB 文件夹副本 => Document/DB/words.csv

我想复制资源文件夹中的 DB 子目录。我认为这个来源很好。但是该来源创建文件夹并且不会复制资源文件夹中 DB 文件夹中的文件。

我真的想复制资源文件夹中 DB 文件夹中的文件。请帮帮我。

【问题讨论】:

【参考方案1】:

1) 不要-autoreleaseNSFileManager。您正在双重释放它,这会使您的应用崩溃。

2) 无需致电-createDirectoryAtPath:。来自-copyItemAtPath:toPath:error:的SDK文档,

srcPath中指定的文件必须存在,而dstPath必须不存在在操作之前

并创建副本失败的目录。

【讨论】:

【参考方案2】:

Swift 3.0

使用字符串

func copyFolder()

    // Get the resource folder
    if let resourceMainPath = Bundle.main.resourcePath

        var isDirectory = ObjCBool(true)
        // Get the path of the folder to copy
        let originPath = (resourceMainPath as NSString).appendingPathComponent("NameOfFolder")
        // Get the destination path, here copying to Caches
        let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        // Append the folder name to dest path so that system creates the directory if it doesnt exist
        let destPath = (destinationPath as NSString).appendingPathComponent("/NameOfFolder")
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: destPath, isDirectory:&isDirectory )
            // If an overwrite behavior is needed, remove and copy again here
             print("Exists")
        else
            // Do the copy
            do 
                try fileManager.copyItem(atPath: originPath, toPath: destPath)
            catch let error
                print(error.localizedDescription)
            
        
    else

    


使用网址

func copyTheFolder()

    // Get the resource folder
    if let resourceMainURL = Bundle.main.resourceURL
        var isDirectory = ObjCBool(true)
        // Get the path of the folder to copy
        let originPath = resourceMainURL.appendingPathComponent("NameOfFolder")
        // Get the destination path, here copying to Caches
        let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        // Append the folder name to dest path so that system creates the directory if it doesnt exist
        let destURL = URL(fileURLWithPath: destinationPath).appendingPathComponent("/NameOfFolder")
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: destURL.path, isDirectory:&isDirectory )
            // If an overwrite behavior is needed, remove and copy again here
            print("Exists")

        else
            // Do the copy
            do 
                try fileManager.copyItem(at: originPath, to: destURL)

            catch let error
                print(error.localizedDescription)
            
        
    

【讨论】:

以上是关于将文件夹从 iPhone 资源目录复制到文档目录的主要内容,如果未能解决你的问题,请参考以下文章

如何编写 Windows 批处理脚本以从目录中复制最新文件?

iOS 将文件从主包复制到文档目录

从文档目录加载 PDF - iPhone

如何在 iPhone 的文档目录中查找内容

将自定义图像(非 JPEG)保存到 iPhone 照片目录

将 NSMutableArray 保存到 iPhone 文档目录