2008-11-13 10 views
11

मैं एक आईओएस एप्लिकेशन विकसित कर रहा हूं, और एप्लिकेशन में बनाई गई फाइल को ज़िप करने का प्रयास कर रहा हूं, क्या ऐसा करने में सक्षम कोई भी फ़ंक्शन है?उद्देश्य सी का उपयोग करके मैं एक ज़िप फ़ाइल कैसे बना सकता हूं?

+0

आपको इसके लिए क्या चाहिए? बस फाइल को संपीड़ित और भेजना? यदि ऐसा है, तो आप पहले से शामिल zlib का उपयोग कर सकते हैं। –

+0

कृपया [यहां] पर अपने जवाब की जांच [1] [1]: http://stackoverflow.com/questions/7386695/how-to-zip-file/18740716#18740716 – Nirmalsinh

उत्तर

9

एलेक्स के रूप में बताया, मैं NSData category Cocoadev विकी के उपयोगकर्ताओं के योगदान की ओर इशारा द्वारा this question का जवाब दिया। उस श्रेणी में एनएसडीएटी इंस्टेंस में ज़िप्ड और जीज़ेड डेटा से निपटने के तरीकों को शामिल किया गया है (जिसे ज़िप फ़ाइल से पढ़ा जा सकता है या एक से लिखा जा सकता है)। जब तक आप अपने फ़ाइल डेटा को NSData इंस्टेंस में फ़ीड कर सकते हैं, तब तक आपको फ़ाइल फ़ाइल को लागू करने की आवश्यकता होनी चाहिए।

कार्रवाई में इस श्रेणी के उदाहरण के लिए, कृपया मेरे आईफोन एप्लिकेशन, Molecules के लिए स्रोत कोड देखें। मैं केवल एक gzipped फ़ाइल (SLSMolecule + PDB.m में) से डेटा निकालने के लिए विधि का उपयोग करता हूं, लेकिन आप उससे मूल अवधारणाएं प्राप्त करने में सक्षम होना चाहिए।

+4

एनएसडीटा श्रेणी का लिंक टूटा हुआ है। –

+1

@GlennHowes - यह यहां वेब संग्रह में उपलब्ध है: http://web.archive.org/web/20100102154917/http://cocoadev.com/index.pl?NSDataCategory, लेकिन मैंने पूरी तरह से शामिल करने के लिए अपने पुराने प्रश्न को संपादित किया श्रेणी को यहां संरक्षित करने के लिए। –

0

सुनिश्चित नहीं है कि कोई अंतर्निहित फ़ंक्शन है, लेकिन शायद आप बाहरी पुस्तकालय जैसे InfoZip का उपयोग कर सकते हैं। या वैकल्पिक रूप से आप अपनी खुद की ज़िप लाइब्रेरी को लागू करने का प्रयास कर सकते हैं, मुझे पूरा यकीन है कि ज़िप प्रारूप विनिर्देश इंटरनेट पर कहीं भी पाया जा सकता है।

RWendi

+0

मेरे द्वारा की गई यह। रॉकेट विज्ञान नहीं, लेकिन टेडियम का एक अच्छा सौदा है। –

2

पहले आप इस उदाहरण में से http://code.google.com/p/objective-zip/downloads/list

ऑब्जेक्टिव-जिप उदाहरण डाउनलोड का पता लगाएं और अपनी परियोजना

आयात दो वर्ग के लिए तीन फ़ोल्डर ऑब्जेक्टिव-पिन, MiniZip और zlib खींचें कॉपी आप में मीटर वर्ग "ZipFile.h" और "ZipWriteStream.h"

मेरी पिन कोड की विधि बनाने है: -

-(IBAction)Zip{ 
self.fileManager = [NSFileManager defaultManager]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES); 

NSString *ZipLibrary = [paths objectAtIndex:0]; 


NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:@"backUp.zip"]; 

//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil]; 

//self.documentsDir = [paths objectAtIndex:0]; 


ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate]; 

NSError *error = nil; 
self.fileManager = [NSFileManager defaultManager]; 
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 

self.documentsDir = [paths1 objectAtIndex:0]; 
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error]; 
//for(NSString *filename in files){ 
    for(int i = 0;i<files.count;i++){ 

     id myArrayElement = [files objectAtIndex:i]; 


    if([myArrayElement rangeOfString:@".png" ].location !=NSNotFound){ 
      NSLog(@"add %@", myArrayElement); 


    NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement]; 
    NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error]; 
    NSDate *Date = [attributes objectForKey:NSFileCreationDate]; 

    ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest]; 
    NSData *data = [NSData dataWithContentsOfFile:path]; 
    // NSLog(@"%d",data); 
    [streem writeData:data]; 
    [streem finishedWriting]; 
     }else if([myArrayElement rangeOfString:@".txt" ].location !=NSNotFound) 
     { 

      NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement]; 
      NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error]; 
      NSDate *Date = [attributes objectForKey:NSFileCreationDate]; 

      ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest]; 
      NSData *data = [NSData dataWithContentsOfFile:path]; 
      // NSLog(@"%d",data); 
      [streem writeData:data]; 
      [streem finishedWriting]; 
    } 
} 

[self testcsv]; 
[zipFile close]; 

} 

अपने दस्तावेज़ों निर्देशिका सहेजा गया आइटम .png और backup.zip साथ लाइब्रेरी फ़ोल्डर में ज़िप करने .txt फ़ाइलें मैं आशा है कि यह है

+0

स्वयं testcsv क्या है ?? – chostDevil

12

मैं निश्चित रूप से ऑब्जेक्टिव-पिन की सिफारिश मदद करता है। यह अभी हाल ही में https://github.com/flyingdolphinstudio/Objective-Zip में ले जाया गया:

एक ज़िप फ़ाइल बनाना::

ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeCreate]; 

एक ज़िप फ़ाइल के लिए एक फ़ाइल जोड़ना:

ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest]; 

[stream writeData:abcData]; 
[stream finishedWriting]; 

अपनी दस्तावेज़ीकरण से कुछ उदाहरण

ज़िप फ़ाइल से फ़ाइल पढ़ना :

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip]; 

[unzipFile goToFirstFileInZip]; 

ZipReadStream *read= [unzipFile readCurrentFileInZip]; 
NSMutableData *data= [[NSMutableData alloc] initWithLength:256]; 
int bytesRead= [read readDataWithBuffer:data]; 

[read finishedReading]; 
+0

एक्सकोड 5.1 और आईओएस 7.1 –

+0

में बहुत अच्छा काम करता है [zipfile close] द्वारा फ़ाइल को बंद करना याद रखें; –