0

所以我正在使用这个库:https ://github.com/flyingdolphinstudio/Objective-Zip

我实现了它,并尝试获取 UIImage 和 NSString 并将其分别设为 .zip 文件中的 .png 和 .txt 。

现在这些是我的两个问题,我正在尝试将下面的 *zipFile 保存到文档目录中。

  1. 现在有了 Dropbox API,为什么我不能只提供文件本身并跳过路径。似乎我必须先将 .zip 保存到文档目录,然后获取路径,然后我才能将其上传到保管箱。我必须这样做吗?

  2. 在 ...writeToFile 行中,我收到一条警告说 ZipFile 可能无法响应 writeToFile 那么我该如何正确地将其保存到文档目录中?

无论如何,这是我到目前为止的代码:

        NSString *filename = [NSString stringWithFormat:@"%@.zip", textField.text];
        ZipFile *zipFile= [[ZipFile alloc] initWithFileName:filename mode:ZipFileModeCreate];

        //Image
        NSString *nameImage = @"Image.png";
        NSMutableDictionary *theDictionary = [Singleton sharedSingleton].dictionary;
        NSData *data = [theDictionary objectForKey:@"image"];
        ZipWriteStream *writeImage = [zipFile writeFileInZipWithName:nameImage compressionLevel:ZipCompressionLevelBest];
        [writeImage writeData:data];
        [writeImage finishedWriting];

        //Text
        NSString *nameText = @"Text.txt";
        NSData *dataText = [textView.text dataUsingEncoding:NSUTF8StringEncoding];
        ZipWriteStream *writeText = [zipFile writeFileInZipWithName:nameText compressionLevel:ZipCompressionLevelBest];
        [writeText writeData:dataText];
        [writeText finishedWriting];

        //Now we HAVE to save it to the documents directory to get it to work with dropbox
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
        NSString *filePath = [documentsPath stringByAppendingPathComponent:filename]; //Add the file name
        [zipFile writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

        //Save to Dropbox
        NSString *zipPath = [[NSBundle mainBundle] pathForResource:textField.text ofType:@"zip"];
        [[self restClient] uploadFile:filename toPath:@"/" withParentRev:nil fromPath:zipPath];

那么我在这里做错了什么?

谢谢!

4

1 回答 1

1

在我看来,它就像ZipFile已经写入文件,所以不需要像writeToFile. 只需使用您想要的路径进行初始化zipFile,确保在末尾关闭文件 ( [zipFile close]),然后像其他文件一样上传到 Dropbox。

于 2013-06-16T17:20:49.873 回答