Not able to unzip folder objective c - iphone

I am using objective-zip for zip/unzip files within folder. my folder contains multiple images, sqlite db file etc. This is the link from where i downloaded
and i got the code also for zip the folder This is the link of that
ziping of folder is done perfectly but i am not able to unzip that. here is my code.
-(void)makeUnZip{
NSString *stringPath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *stringPath=[stringPath1 stringByAppendingPathComponent:[#"test" stringByAppendingFormat:#".zip"]];
ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:stringPath mode:ZipFileModeUnzip];
NSArray *infos = [unzipFile listFileInZipInfos];
for (NSString *file in infos) {
ZipReadStream *readStream = [unzipFile readCurrentFileInZip];
FileInZipInfo *fileInfo = [unzipFile getCurrentFileInZipInfo];
NSString *fileName = [fileInfo name];
NSLog(#"File Name--- %#",fileName);
NSString *unzipFilePath = [stringPath1 stringByAppendingPathComponent:fileName];
NSString *dir = [unzipFilePath stringByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:nil]) {
[[NSFileManager defaultManager] createDirectoryAtPath:dir attributes:nil];
NSLog(#"created directory %#", dir);
}
}
[unzipFile close];
}
This is link from where i got the reference for unzip
NSLog shows only one file name for all the file and my ziped file of document directory is not unziping.
For example my zip folder contains 3 images abc1.png, abc2.png and abc3.png then NSLog shows abc3.png for all 3 and it is not unziping.
When i am unzipping manually by going to simulator folder then it is showing all the images perfectly.
Can any one look in too it and let me know where I am wrong ?

Finally i have done it with SSZipArchive
Not found enough help for objective-zip.
Thanks kirti mali and vijay to comment.

Related

Renaming and moving file using NSFIleManager

This is my code to rename and move file from a temporary directory to My Documents directory.
So the problem is within two sections of mode Create a Directory and Attempt to move. The rest all is working correctly..
When I comment out the Create directory section, the Attempt to Move section logs in the console:
Unable to Move the File..Error Code 4.
Now I researched error code 4 and it means that the directory does not exist. So I added the code to create the directory.
Now when I run the program the directory is created but the Attempt to Move section logs:
Unable to Move File..error code 512
Now researching on it it is due to the file already exists. The destination must not exist.
So I am pretty confused, since both error codes are contracting each other.
{
NSError *error;
NSFileManager* manager = [[NSFileManager alloc] init];
NSString* tempFile =[NSTemporaryDirectory() stringByAppendingPathComponent:#"recordTest.caf"];
if (tempFile)
{
// Get the Documents Directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSLog(#"Documents directory: %#",
[manager contentsOfDirectoryAtPath:documentsDirectory error:&error]);
//Get the User File Save Name from the text field
NSString *UserText = [[self FileNameText] text];
// Rename the file, by moving the file
NSString *filePath2 = [documentsDirectory
stringByAppendingPathComponent:[NSString stringWithString:UserText]];
// Create a Directory
if( [manager createDirectoryAtPath:[NSString stringWithFormat:#"%#/%#",documentsDirectory,UserText] withIntermediateDirectories:NO attributes:nil error:&error]!= YES)
{
NSLog(#"Directory error");
}
if([manager fileExistsAtPath:[NSString stringWithFormat:#"%#/%#",documentsDirectory,UserText]])
{
NSLog(#"Path exist");
NSLog(#"Documents directory: %#",
[manager contentsOfDirectoryAtPath:documentsDirectory error:&error]);
}
// Attempt the move
if ([manager moveItemAtPath:tempFile toPath:filePath2 error:&error] != YES)
{
NSLog(#"Unable to move file: %#", [error localizedDescription]);
}
else
{
if ([manager removeItemAtPath:tempFile error:&error] != YES)
NSLog(#"Unable to delete file: %#", [error localizedDescription]);
}
}
[manager release];
}
The problem is most likely that the file you are trying to move doesn't exist.
replace
if (tempFile) //this will return YES every time, telling you you have a string!
with
if ([manager fileExistsAtPath:tempFile]) //this will tell you if the file exists at that path
A few notes
1. To see with your eyes if the file you are looking for really exists, check the simulator application folders at something similar to this:
/Users/yogevshelly/Library/Application\ Support/iPhone\
Simulator/5.0/Applications
then browse to a specific application
2. Don't just append strings to get folders , do something like this:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [dirPaths objectAtIndex:0];
3. The following line will create a folder with the file's name as the folders name!:
[manager createDirectoryAtPath:filePath2 withIntermediateDirectories:NO attributes:nil error:&error]
creating a folder from a filename seems weird, what you want to do is either copy the file to an existing directory or first create a directory and copy the file into-it
you use the metod
contentsOfDirectoryAtPath: withIntermediateDirectories: attributes:error
this method create directory but you pass it the filename.
i think you create /Documents/filename/ instead of /Documents/filename
As per my understanding the problem part of the code is following one
// Create a Directory
if( [manager createDirectoryAtPath:[NSString stringWithFormat:#"%#/%#",documentsDirectory,UserText] withIntermediateDirectories:NO attributes:nil error:&error]!= YES)
{
NSLog(#"Directory error");
}
You are creating a folder in with the file name in same location .... try commenting the the above code .. it should work.

Problems with updating database with iPhone

I will try describe my problem as best as I can. I have created a sqlite database using the console on the mac. I've added the file to my project, by right clicking and hitting "add files to (projectname)" and it adds the database. I am able to read the database, however when I try and update the database, although my debugging has confirmed the statements (update statements) succeed, the data in the database is not updating. I am gathering this is because the pointer to the database file is pointing to the .sql file in my documents (which is where the original file that I added is located), however I thought by adding the file to the project, a copy of that file would go within the project folder.
My question is how can I add a .sql file to my project where it is not referenced from the desktop, and be able to update the information that is located within the database.
No the file is not the one on your desktop, the file be bundled in the .app bundle.
The database is located in the app bundle which is readonly, you will need to move the database to the document directory on first startup.
place something like this in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];
NSString *sqlFile =[documentDirectory stringByAppendingPathComponent:#"yourdb.sql"];
if (![[NSFileManager defaultManager] fileExistsAtPath:sqlFile]) {
NSString *bundleSql = [[NSBundle mainBundle] pathForResource:#"yourdb" ofType:#"sql"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:bundleSql toPath:sqlFile error:&error];
if (error) {
NSLog(#"Could not copy json file: %#", error);
}
}
Get your SQL file's directory path on your bundle by :
NSString *sqlFilePath = [[NSBundle mainBundle] pathForResource:#"YourSQLFileName" ofType:#"sql"];
Next copy the sql file to your sandbox directory using this command :
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sandboxDirectoryPath = [documentPaths objectAtIndex:0];
NSString *newSQLFilePath = [NSString stringWithFormat:#"%#/YourSQLFileName.sql", sandboxDirectoryPath];
[[NSFileManager defaultManager] copyItemAtPath:sqlFilePath toPath:newSQLFilePath error:error]

iPhone - Getting zip file name and folder same as that of zip name

I'm using the following code to get list of zip files from a folder in my iphone application.
NSFileManager* fileMan = [NSFileManager defaultManager];
NSArray* files = [fileMan contentsOfDirectoryAtPath:myFolder error:&err];
NSArray* exts = [NSArray arrayWithObjects:#"zip", nil];
files = [files pathsMatchingExtensions:exts];
I'm using above code to remove zip files in "myFolder"
In "myFolder", along with my zip, i also have unzip folder with the same name. For example if zip file name is A.zip, unzip folder name is "A".
My question is how can I get "A.zip" and "A". I don't want to have code that extracts the file name and then getting the folder...... I want to know if any other alternative is there
Use the following codes.
NSString *fileName = [[path lastPathComponent] stringByDeletingPathExtension];
NSString *fileType = [path pathExtension];
where path is a NSString.

problem in reading files in iphone application!

I am reading a file in iphone app. Its Works Fine.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"%#",documentsDirectory);
NSString *fileName = [NSString stringWithFormat:#"%#/test.txt",documentsDirectory];
NSLog(#"%#",fileName);
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
NSLog (#"%#",content);
}
works good when i am using filename as test.txt
But when i add another file in the resource suppose test1.txt then NSLog(#"%#",documentsDirectory) and NSLog(#"%#",fileName) shows the right result. But
NSLog (#"%#",content); prints null in the log. So what is the reason?
I am printing detail error and it prints
NSFilePath = "/Users/sam-xxx/Library/Application Support/iPhone Simulator/3.2/Applications/51197946-6042-4A90-AA39-F07F8A649308/Documents/test1.txt";
NSUnderlyingError = Error Domain=NSPOSIXErrorDomain Code=2 "Operation could not be completed. No such file or directory";
It would be best here to check to see if an error is returned:
NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error: &error];
if (error) NSLog(#"Error !: %#", [error localizedDescription]);
That will (hopefully) give you a clue as to whats going on.
(Edited to give example bundle resource usage as the file is in the bundle not the Documents directory).
Docs for NSBundle are here: NSBundle Documentation
You have 2 choices, the one you suggest:
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
Which will return the Bundle resource directory with the filename appended to that path.
Personally I prefer the pathForResource:ofType: method:
NSString *filename = [[NSBundle mainBundle] pathForResource: #"test1" ofType: #"txt"];
As this will not only tell you if the file exists (returns nil if it does not) but will also search the localisation directories if you have them.
Did you check if the file in the error message exists? When run in the simulator you can simply open Finder, press cmd+shift+g and paste the directory to the file.
Maybe you didn't add it to the target? Open the information window for the file in your resources and check which targets are checked.
When you run a app in Simulator, you could copy and paste the right file into the app's Documents directory use Finder, but on iPhone or iTouch how could you paste a file into the Documents directory.
We got only 2 method to put something into Documents directory when app is running on iPhone or iTouch.
Create one.
Copy one from the bundle.
in your code, you read a file in the Documents directory, you could be paste a file in the directory manually use Finder. Try this.
NSString *path = [[NSBundle mainBundle] pathForResource:#"test.txt" ofType:nil];
NSString *content = [[NSString alloc] initWithContentsOfFile:path
usedEncoding:nil
error:nil];
NSLog (#"%#",content);

iPhone: Can access files in documents directory in Simulator, but not device

I'm writing an app that copies some contents of the bundle into the applications Document's directory, mainly images and media. I then access this media throughout the app from the Document's directory.
This works totally fine in the Simulator, but not on the device. The assets just come up as null. I've done NSLog's and the paths to the files look correct, and I've confirmed that the files exist in the directory by dumping a file listing in the console.
Any ideas? Thank you!
EDIT
Here's the code that copies to the Document's directory
NSString *pathToPublicationDirectory = [NSString stringWithFormat:#"install/%d",[[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]];
NSString *manifestPath = [[NSBundle mainBundle] pathForResource:#"content" ofType:#"xml" inDirectory:pathToPublicationDirectory];
[self parsePublicationAt:manifestPath];
// Get actual bundle path to publication folder
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathToPublicationDirectory];
// Then build the destination path
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", [[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]]];
NSError *error = nil;
// If it already exists in the documents directory, delete it
if ([fileManager fileExistsAtPath:destinationPath]) {
[fileManager removeItemAtPath:destinationPath error:&error];
}
// Copy publication folder to documents directory
[fileManager copyItemAtPath:bundlePath toPath:destinationPath error:&error];
I am figuring out the path to the docs directory with this method:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
And here's an example of how I'm building a path to an image
path = [NSString stringWithFormat:#"%#/%d/%#", [self applicationDocumentsDirectory], [[thisItem valueForKey:#"publicationID"] intValue], [thisItem valueForKey:#"coverImage"]];
It turned out to be an issue where the bundle was not being updated on the device and apparently didn't have the same set of files that the Simulator had. This blog post helped a bit: http://majicjungle.com/blog/?p=123
Basically, I cleaned the build and delete the app and installed directly to the device first instead of the simulator. Interesting stuff.
I don't see where documentsDirectory is defined.
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", [[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]]];
Perhaps the following will do the trick
NSString *destinationPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", [[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]]];
Your copy code looks fine, and you say you're getting no errors.
But I'm intrigued by "The assets just come up as null." Are you sure you're accessing the file name later with the exact same name?
Unlike the simulator, a real iPhone has a case sensitive file system.