I would like to save a doc file into a zip file in the document directory and when the user chooses they can attach it in an email. I have created the zip file but when I want to open it again it creates a zipfile, zip.cpgz
instead of testing.zip
.
Here is my code for creating the zip file
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];
}
}
You must use ssziparchive
An Utility class for zipping and unzipping files for iOS
Example provided:-
// 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];