I have a zip file in NSMainBuddle (code.zip) inside code.zip two file names.txt,group.txt, now i want to add one more file to code.zip file, ie i am trying to add Demo.txt.
- (IBAction)CreatezipAction:(id)sender{
ZipFile *zipFile= [[ZipFile alloc]initWithFileName:[[NSBundle mainBundle] pathForResource:@"code" ofType:@"zip"] mode:ZipFileModeCreate];
ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"Demo.txt" compressionLevel:ZipCompressionLevelBest];
NSString* str= @"Sample String…………";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
[stream writeData:data];
[stream finishedWriting];
}
its successfully adding to NSMainBunble because if i click button corresponding to lisAllFileAction: its will show like this….
- (IBAction)lisAllFileAction:(id)sender {
ZipFile *unzipFile= [[ZipFile alloc]initWithFileName:[[NSBundle mainBundle] pathForResource:@"code" ofType:@"zip"] mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);
}
}
output on simulator
2
012-06-04 11:55:42.143 zipDemo[1757:207] - code-Zip/names.txt 2012-5-30 17:19:00 +0000 983 (-1)
2012-06-04 11:55:42.143 zipDemo[1757:207] - code-Zip/names.txt 2012-06-01 20:07:50 +0000 2563 (-1)
2012-06-04 11:55:42.144 zipDemo[1757:207] - Demo.txt 2012-06-04 06:25:40 +0000 12 (9)
- Now i want to know can manually open this Demo.txt?
- what is the difference b/w code-Zip/names.txt 2012-06-01 20:07:50 and Demo.txt 2012-06-04 06:25:40 +0000 12 (9)?
- Can create file in in NSMainBundle like this, any problem for app?
- Actually where Demo.txt file created?
The
mainBundleis read-only. You can’t write nothing in the mainBundle, you can just load. You should create your files in theDocumentsorCachesdirectory. So, the first time the app starts, you can copy the zip file from the mainBundle to the Documents directory, and then use this instead the zip file in the mainBundle.