Ziparchive not working for unzip in iphone - ios

I am working on zip and unzip object, and for that, I am using ziparchive classes ,I am able to zip files but problem occurs while unzipping of file ,following is my code
NSString *str1;
ZipArchive *za = [[ZipArchive alloc] init];
[za CreateZipFile2:zipFile];
NSLog(#"this is my whole new ipad%d",pictureArr.count);
[self calculation];
for (int i=0;i<[pictureArr count]; i++) {
NSString *str = [NSString stringWithFormat:#"%#",[pictureArr objectAtIndex:i]];
NSLog(#"This is my whole new ipad%#",[pictureArr objectAtIndex:i]);
[za addFileToZip:str newname:[pictureArr objectAtIndex:i]];
}
//NSString *textPath = [cachePath stringByAppendingPathComponent:#"text.txt"];
NSString *strUrl = [NSString stringWithString:path];
NSString *strunzip=[docspath stringByAppendingPathComponent:strUrl];
NSLog(#"This is my new file path for strXmlName%#",docspath);
[za addFileToZip:strUrl newname:strXmlName];
NSString *fileContent = [docspath stringByAppendingPathComponent:#"strXmlName.zip"];
NSLog(#"This is my final path for%#",fileContent);
NSString *updateURL = [[NSBundle mainBundle] pathForResource:fileContent ofType:#"zip" inDirectory:#"res"];
if ([za UnzipOpenFile:fileContent]) {
if ([za UnzipFileTo:docspath overWrite:NO]) {
NSLog(#"Archive unzip success");
[fileManager removeItemAtPath:docspath error:NULL];
}
else {
NSLog(#"Failure to unzip archive");
}
/*7405726724,9227669500-agile infoway
*/
}
else {
NSLog(#"Failure to open archive");
}
NSData *unzipData = [NSData dataWithContentsOfFile:fileContent];
fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:docspath contents:unzipData attributes:nil];
NSLog(#"This is my file");
[za UnzipOpenFile:path];
BOOL success = [za CloseZipFile2];
NSLog(#"Zipped file with result %d",success);
but it directly goes out from if ([za UnzipOpenFile:fileContent]) i dont know why its happning,
what i have tried is this
and this
so please guide me
thanks

Related

How to unzip a downloaded data and use a particular file from the unzipped file (index.html) to load in a UIWebview

I am stuck with unziping a zip file and fetch a paritcular file from that unzipped content (index.html) and finally use that index.html to load a UIWebview .
Am using GZIP library to unzip the file which am downloading
[data gunzippedData];
Is the method which am using to unzip the data using GZIP .
For unzipping the file from web use the below code.
Dowbload ZipArchive.h and #import in your file.
NSURL *urlVersion = [NSURL URLWithString: [tempDirOuter objectForKey:#"filename"]];
NSLog(#"txt File : %#",urlVersion);
NSError *error = nil;
// 2
NSData *data = [NSData dataWithContentsOfURL:urlVersion options:0 error:&error];
NSString *textFilePath;
if(!error)
{
// 3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *strFileName = [tempDirOuter objectForKey:#"fileNameOnly"];
NSString *zipPath = [path stringByAppendingPathComponent:strFileName];
[data writeToFile:zipPath options:0 error:&error];
ZipArchive *za = [[ZipArchive alloc] init];
// 1
if ([za UnzipOpenFile: zipPath])
{
// 2
BOOL ret = [za UnzipFileTo: path overWrite: YES];
if (NO == ret){} [za UnzipCloseFile];
// 3
NSString *StrName = [strFileName stringByReplacingOccurrencesOfString:#".zip" withString:#".txt"];
textFilePath = [path stringByAppendingPathComponent:StrName];
}
if(!error)
{
// TODO: Unzip
}
else
{
NSLog(#"Error saving file %#",error);
}
}
else
{
NSLog(#"Error downloading zip file: %#", error);
}
NSString *webVersion = [NSString stringWithContentsOfFile:textFilePath encoding:NSASCIIStringEncoding error:nil];
In string webVersion you will get the html path.

iOS write to a txt [duplicate]

This question already has an answer here:
How to write in a txt-file, iOS 7
(1 answer)
Closed 8 years ago.
I want to write in a txt, but my code not working...
This is my code:
-(void)bestScore{
if(cptScore > bestScore){
bestScore = cptScore;
highScore.text =[[NSString alloc] initWithFormat: #" %.d", bestScore];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"bestScore" ofType:#"txt"];
NSString *test = #"test";
[test writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
}
I have already a file named bestScore.txt in my folder "Supporting Files"
Can you help me please ?
Thanks
EDIT :
I can read my file "bestScore.txt" with this code :
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"bestScore" ofType:#"txt"];
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
highScore.text = textFromFile;
bestScore = [textFromFile intValue];
Try this:
-(void)bestScore{
if(cptScore > bestScore){
bestScore = cptScore;
highScore.text =[[NSString alloc] initWithFormat: #" %.d", bestScore];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Library/Preferences/bestScore.txt"];
NSString *test = [NSString stringWithFormat:#"%#",bestStore];
NSError *error;
// save a new file with the new best score into ~/Library/Preferences/bestScore.txt
if([test writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error])
{
// wrote to file successfully
NSLog(#"succesfully wrote file to %#", filePath);
} else {
// there was a problem
NSLog(#"could not write file to %# because %#", filePath, [error localizedDescription]);
}
}
}
And to read the score back in, you can do:
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Library/Preferences/bestScore.txt"];
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
if(textFromFile)
{
highScore.text = textFromFile;
bestScore = [textFromFile intValue];
} else {
// if the file doesn't exist or if there's an error, initialize bestScore to zero
bestScore = 0;
highScore.text = #"";
}

ios - transfer In App Purchase downloaded file to Document folder

I successfully test my in app purchase function and downloaded the content. However I am not sure of an right way to transfer it to Document folder. I tested with the code:
-(void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads
{
for (SKDownload *download in downloads)
{
switch (download.downloadState) {
case SKDownloadStateActive:
NSLog(#"Download progress = %f",
download.progress);
NSLog(#"Download time = %f",
download.timeRemaining);
break;
case SKDownloadStateFinished:
[self downloadFromURL:download.contentURL];///<--This is the function.
break;
}
}
}
-(void)downloadFromURL: (NSURL *) temporaryURL {
NSString *folderName = [[temporaryURL path] lastPathComponent];
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folder = [pathArr objectAtIndex:0];
NSString *filePath = [folder stringByAppendingPathComponent:folderName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *writeError = nil;
NSData *downloadData = [[NSData alloc] initWithContentsOfURL:temporaryURL];
[downloadData writeToURL: fileURL options:0 error:&writeError];
if( writeError) {
NSLog(#"Error in writing file %#' : \n %# ", filePath , writeError);
return;
}
NSLog(#"File successfully downloaded. Url is %#",fileURL.absoluteString);
//myFileURL = fileURL;
NSFileManager *filemgr;
NSArray *filelist;
int count;
int i;
filemgr =[NSFileManager defaultManager];
filelist = [filemgr contentsOfDirectoryAtPath:folder error:NULL];
count = [filelist count];
NSLog(#"file count %i",count);
for (i = 0; i < count; i++)
NSLog(#"%#", filelist[i]);
}
I get a #"File successfully downloaded. Url is %#" from the code above but NSLog(#"%#", filelist[i]); give me no content. What else is missing here? I found out about this link
but not sure how to incorporate into my code. Any pointer will be good.
Let's check:
-(void)downloadFromURL: (NSURL *) temporaryURL {
// at this time, file is downloaded successfully???
// I think you should check here
NSURL * temporaryURL;
NSFileManager *fm = [NSFileManager defaultManager];
BOOL existed = [fm fileExistsAtPath:temporaryURL.path];
if(existed) // file is existed
else // file is not existed == maybe download fails
}

Retrieving files with read attribute and classifying them

I have gotten this code from somewhere else with a little editing which allows to go through all the directories with the starting path specified using enumeratorAtPath. With this code i am able to retrieve files with a certain extension which has read attribute using isReadableFileAtPath. What i would like is to retrieve all files with any extension which has read attribute and classify them into different categories based on their last folder name.
Example if i have a path /a/b/c/example.plist, /a/b/c/example.plist would be classified under c.
- (NSString *) retrievePaths
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableString *filePaths = [[NSMutableString alloc] init];
NSDirectoryEnumerator *dirnum = [[NSFileManager defaultManager] enumeratorAtPath:#"/"];
NSString *nextItem = [NSString string];
int i = 0;
while( (nextItem = [dirnum nextObject]))
{
if([[nextItem pathExtension] isEqualToString:#"db"] || [[nextItem pathExtension] isEqualToString:#"sqlitedb"] || [[nextItem pathExtension] isEqualToString:#"sqlite"]) {
if([fileManager isReadableFileAtPath:nextItem])
{
i++;
[filePaths appendFormat:#"%d)",i];
[filePaths appendString:nextItem];
[filePaths appendString:#"\n"];
}
}
}
NSString *format = [NSString stringWithFormat:#"Paths : %#", filePaths];
return format;
}
Remove below if condition from your code and you will be able to get all files with readable access permission
if([[nextItem pathExtension] isEqualToString:#"db"] || [[nextItem pathExtension] isEqualToString:#"sqlitedb"] || [[nextItem pathExtension] isEqualToString:#"sqlite"])
EDIT
Updated function try this, will work for you..
- (NSString *) retrievePaths
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableString *filePaths = [[NSMutableString alloc] init];
NSString *path = #"/Assign/your/path/"; // assign your path
NSDirectoryEnumerator *dirnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
NSString *nextItem = [NSString string];
int i = 0;
while( (nextItem = [dirnum nextObject]))
{
NSString *filePath = [path stringByAppendingPathComponent:nextItem];
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];
if(NO == isDir) {
if([fileManager isReadableFileAtPath:filePath])
{
i++;
[filePaths appendFormat:#"%d)",i];
[filePaths appendString:nextItem];
[filePaths appendString:#"\n"];
}
}
}
NSString *format = [NSString stringWithFormat:#"Paths : %#", filePaths];
return format;
}

How can I return all the name of the files in Resources folder in ios?

I want to retrieve in an array of the name of the files in Resources folder that have the .html extension. I have done this:
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Resources"];
NSError *error = nil;
NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
for (int i=0; i < [documentArray count] - 1; i++) {
NSLog(#"value %#", [documentArray objectAtIndex:i]);
}
But the for loop displays continously null. Anyone knows how can I get this task right? thank you
Don't use NSHomeDirectory. Use [[NSBundle mainBundle] resourcePath] to get path to app's resources.
Edited: here's your example code.
NSString *resPath = [[NSBundle mainBundle] resourcePath];
NSError *error = nil;
NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resPath error:&error];
if (!error)
{
for (NSString * filename in filenames)
{
NSString *extension = [filename pathExtension];
if ([extension isEqualToString:#"html"])
{
NSLog(#"%#", filename);
}
}
}

Resources