2008-11-13 26 views
11

Tôi đang phát triển một ứng dụng iOS và cố gắng nén tệp mà tôi đã tạo trong ứng dụng, có chức năng tích hợp nào có thể thực hiện việc này không?Làm cách nào để tạo tệp zip bằng cách sử dụng Objective C?

+0

Bạn cần điều này để làm gì? Chỉ cần nén và gửi tệp ở đâu đó? Nếu vậy, bạn có thể sử dụng zlib được bao gồm rồi. –

+0

Vui lòng kiểm tra câu trả lời của bạn tại [đây] [1] [1]: http://stackoverflow.com/questions/7386695/how-to-zip-file/18740716#18740716 – Nirmalsinh

Trả lời

9

Như Alex chỉ ra, tôi trả lời this question bằng cách chỉ ra các NSData category góp của người sử dụng wiki Cocoadev. Danh mục đó bao gồm các phương thức để xử lý dữ liệu nén và nén trong một cá thể NSData (có thể đọc từ một tệp Zip hoặc được ghi vào một tệp). Đây sẽ là tất cả những gì bạn cần để thực hiện nén tệp mà bạn mô tả, miễn là bạn có thể cấp dữ liệu tệp của mình vào một cá thể NSData.

Để biết ví dụ về danh mục này đang hoạt động, vui lòng xem mã nguồn cho ứng dụng iPhone của tôi, Molecules. Tôi chỉ sử dụng phương pháp để trích xuất dữ liệu từ một tập tin nén (trong SLSMolecule + PDB.m), nhưng bạn sẽ có thể nhận được các khái niệm cơ bản từ đó.

+4

Liên kết đến danh mục NSData bị hỏng. –

+1

@GlennHowes - Nó có sẵn trong kho lưu trữ web tại đây: http://web.archive.org/web/20100102154917/http://cocoadev.com/index.pl?NSDataCategory, nhưng tôi đã chỉnh sửa câu hỏi cũ của mình để bao gồm toàn bộ của danh mục để bảo tồn nó ở đây. –

0

Không chắc chắn nếu có chức năng tích hợp sẵn, nhưng có lẽ bạn có thể sử dụng thư viện bên ngoài như InfoZip. Hoặc cách khác bạn có thể thử thực hiện thư viện zip của riêng bạn, tôi khá chắc chắn Zip định dạng đặc điểm kỹ thuật có thể được tìm thấy ở đâu đó trên internet.

RWendi

+0

tôi đã thực hiện nó. Không phải khoa học tên lửa, mà là rất nhiều tedium. –

2

đầu tiên bạn tải về Objective-zip ví dụ từ http://code.google.com/p/objective-zip/downloads/list

trong ví dụ này Tìm và sao chép ba thư mục Objective-Zip, MiniZip và ZLib kéo trong dự án của bạn

nhập khẩu hai lớp trong bạn .m lớp "ZipFile.h" và "ZipWriteStream.h"

tạo phương pháp nén mã của tôi là: -

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

} 

thư mục tài liệu của bạn mục đã lưu .png và các tập tin .txt nén trong thư mục Library với backup.zip tôi hy vọng điều này là giúp

+0

tự testcsv là gì ?? – chostDevil

12

Tôi chắc chắn khuyên Objective-Zip. Nó chỉ vừa mới chuyển đến https://github.com/flyingdolphinstudio/Objective-Zip:

Một số ví dụ từ tài liệu của họ:

Tạo một file Zip:

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

Thêm một tập tin vào một tập tin zip:

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

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

Đọc tệp từ tệp zip :

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

Hoạt động tốt trong Xcode 5.1 và iOS 7.1 –

+0

Hãy nhớ đóng tệp bằng [zipfile close]; –

Các vấn đề liên quan