2015-05-08 6 views
0

Я хотел бы сохранить файл doc в zip-файле в каталоге документа, и когда пользователь захочет, он может подключить его по электронной почте. Я создал zip-файл, но когда я хочу его снова открыть, он создает zipfile, zip.cpgz вместо testing.zip.Заблокировать файл doc в ios и сохранить в каталоге документов

Вот мой код для создания архива

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; 

int index =0; 
for (NSString *item in filePathsArray){ 
    if ([[item pathExtension] isEqualToString:@"doc"]) 
    { 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:index]]; 
     //NSString *FileName=[stringPath1 stringByAppendingPathComponent:@"Console.doc"]; 

     NSString *stringPath=[documentsDirectory stringByAppendingPathComponent:[@"testing" stringByAppendingFormat:@".zip"]]; 

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


     ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"doc" compressionLevel:ZipCompressionLevelBest]; 
     NSData *data = [NSData dataWithContentsOfFile:filePath]; 
     [stream writeData:data]; 
     [stream finishedWriting]; 

    } 
} 
+1

Непонятно, что вы хотите. Перепишите это. – trojanfoe

+0

На самом деле я хочу сжать файл документа и сохранить его, чтобы сделать каталог документов – RaviJSS

ответ

1

Вы должны использовать ssziparchive

класс

утилита для архивирования и разархивирования файлов для прошивки

Примера при условии: -

// Unzipping 
NSString *zipPath = @"path_to_your_zip_file"; 
NSString *destinationPath = @"path_to_the_folder_where_you_want_it_unzipped"; 
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath]; 


    // Zipping 
    NSString *zippedPath = @"path_where_you_want_the_file_created"; 
    NSArray *inputPaths = [NSArray arrayWithObjects: 
          [[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"], 
          [[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"] 
          nil]; 
    [SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths]; 
+0

Это не отвечает на вопрос; проблема не в классе zip, а в использовании OP класса 'NSFileManager'. – trojanfoe

+0

Прошу прощения, вы не понимаете, о чем спрашивает OP, он сказал, что уже нашел способ заархивировать файл, но не найти, как распаковать или извлечь его. Пожалуйста, внимательно прочитайте, прежде чем говорить. Просто вниз голосование ответ будет просто вычитать «-1» только от вашей репутации. – Vizllx

+1

Я не ответил на ваш ответ. И я не твой помощник. – trojanfoe

Смежные вопросы