将 doc 文件压缩到 ios 并保存到 document 目录

Zip the doc file in ios and save in to document directory

我想将 doc 文件保存到文档目录中的 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];

    }
}

你必须使用ssziparchive

用于压缩和解压缩 iOS

文件的实用程序 class

提供的示例:-

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