0

So I am using this library: https://github.com/flyingdolphinstudio/Objective-Zip

I implemented it and am trying to take a UIImage and NSString and make it a .png and .txt in the .zip file, respectively.

Now these are my 2 concerns, I am trying to save the *zipFile below to the documents directory.

  1. Now with the dropbox API, how come I can't just provide the file itself and skip the path. It seems like I HAVE to save the .zip to the documents directory first and then get the path so I can then upload it to dropbox. Do I have to do that?

  2. In the ...writeToFile line, I am getting a warning that ZipFile may not respond to writeToFile so how would I properly save it to the documents directory?

Anyway this is the code I have so far:

        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];

So what am I doing wrong here?

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • 1
    "I am getting a warning that ZipFile may not respond to writeToFile so how would I properly save it to the documents directory?" - perhaps read its documentation? –  Jun 16 '13 at 06:53
  • If this is the documentation link your talking about: https://github.com/flyingdolphinstudio/Objective-Zip/blob/master/GETTING_STARTED.md , then there is nothing there that states how to save it. Can you link to where you found the answer? – SimplyKiwi Jun 16 '13 at 17:18

1 Answers1

1

It looks to me like ZipFile already writes to a file, so there's no need for something like writeToFile. Just initialize zipFile with the path you want, be sure to close the file at the end ([zipFile close]), and then upload to Dropbox as you would any other file.

user94559
  • 59,196
  • 6
  • 103
  • 103
  • Thanks for the clarification but the zipFile path is still unclear to me. How would I get that path to feed it into dropbox's fromPath paramater? – SimplyKiwi Jun 16 '13 at 17:40
  • You'd just feed it the path you used when you created the `zipFile` object. In the above code, you appear to be using `[NSString stringWithFormat:@"%@.zip", textField.text]` as the filename. – user94559 Jun 17 '13 at 01:21
  • Ok I understand. On another hand, I keep getting a crash in the initWithFileName line with the crash saying it can't open 'test.zip' (If I named it test.zip). But, I am clearly setting it to the ZipFileModeCreate mode so I am not sure why this is happening. Do you have any idea? – SimplyKiwi Jun 17 '13 at 02:41
  • No, I don't know. I've never used Objective-Zip before. – user94559 Jun 17 '13 at 16:11
  • Just a heads up to you and anyone else who is having a similar issue, re-writing the zip code from this question fixed that issue: http://stackoverflow.com/questions/7809359/how-can-i-convert-my-zip-file-to-nsdata-to-email-my-zip-file-as-an-attachment – SimplyKiwi Jun 19 '13 at 05:14