Заархивируйте файл doc в ios и сохраните в каталоге документов

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

Вот мой код для создания zip-файла

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 ответ

Решение

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

Класс Utility для архивирования и разархивирования файлов для iOS

Пример предоставлен:-

// 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];
Другие вопросы по тегам