I am developing an iOS application, and trying to zip the file I have created in the application, is there any built in function able to do this?
I definitely recommend Objective-Zip. It just recently moved to https://github.com/flyingdolphinstudio/Objective-Zip:
Some examples from their documentation:
Creating a Zip File:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:#"test.zip" mode:ZipFileModeCreate];
Adding a file to a zip file:
ZipWriteStream *stream= [zipFile writeFileInZipWithName:#"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];
Reading a file from a zip file:
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:#"test.zip" mode:ZipFileModeUnzip];
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
As Alex pointed out, I responded to this question by pointing out the NSData category contributed by users of the Cocoadev wiki. That category includes methods for dealing with zipped and gzipped data in an NSData instance (that could be read from a Zip file or written to one). This should be all you need to implement the file zipping you describe, as long as you can feed your file data into an NSData instance.
For an example of this category in action, please see the source code for my iPhone application, Molecules. I only use the method to extract data from a gzipped file (in SLSMolecule+PDB.m), but you should be able to get the basic concepts from that.
first you download Objective-zip example from http://code.google.com/p/objective-zip/downloads/list
in this example Find and copy three folder Objective-Zip, MiniZip and ZLib drag in to your project
import two class in you .m class
"ZipFile.h" and
"ZipWriteStream.h"
create method of zip my code is :-
-(IBAction)Zip{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);
NSString *ZipLibrary = [paths objectAtIndex:0];
NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:#"backUp.zip"];
//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil];
//self.documentsDir = [paths objectAtIndex:0];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate];
NSError *error = nil;
self.fileManager = [NSFileManager defaultManager];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
self.documentsDir = [paths1 objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error];
//for(NSString *filename in files){
for(int i = 0;i<files.count;i++){
id myArrayElement = [files objectAtIndex:i];
if([myArrayElement rangeOfString:#".png" ].location !=NSNotFound){
NSLog(#"add %#", myArrayElement);
NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
// NSLog(#"%d",data);
[streem writeData:data];
[streem finishedWriting];
}else if([myArrayElement rangeOfString:#".txt" ].location !=NSNotFound)
{
NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
// NSLog(#"%d",data);
[streem writeData:data];
[streem finishedWriting];
}
}
[self testcsv];
[zipFile close];
}
your documents directory saved item .png and .txt files zipping in Library folder with backup.zip
i hope this is helps
Related
It would seem that my apps no longer are able to save files to their documents folder when doing this:
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *dta = [[NSData alloc] init];
dta = [NSData dataWithContentsOfFile:path];
NSLog(#"writing file: %#", path);
if ([dta writeToFile:path options:NSAtomicWrite error:nil] == NO) {
NSLog(#"writeToFile error");
}else {
NSLog(#"Written: %#", path);
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
I do get "Written" in the log with the full path:
Written: /var/mobile/Containers/Data/Application/ECBCD65D-A990-4758-A07F-ECE48E269278/Documents/9d440408-4758-4219-9c9c-12fe69cf82f2_pdf.pdf
And as long as I don't quit the app I can load the PDF in a UIWebView like this (pdfPath is a string that I pass to the function that loads the PDF file):
[www loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pdfPath]]];
Any help at all is as always greatly appreciated.
Okay, after mucking about I found that since the documents directory changes path every time I run the app, I had to:
Write the file to the documents folder with the complete path to
the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: fileName];
NSData *dta = [[NSData alloc] init];
dta = [NSData dataWithContentsOfFile:path];
if ([dta writeToFile:path options:NSAtomicWrite error:nil] == NO) {
NSLog(#"writeToFile error");
}else {
NSLog(#"Written: %#", path);
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
Save only the file name to my database
Load the file by using the full newly generated Documents directory path and append the file name from my database:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: pdfPath];
Dropbox restClient saves only files. So i want to save the image in local folder first and then to upload it, as a result it saves file, but it is corrupted.
NSString *localPath = [[NSBundle mainBundle] pathForResource:#"Info" ofType:#"plist"];
NSString *jpegFilePath = [NSString stringWithFormat:#"%#/test.jpeg",localPath];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data2 writeToFile:jpegFilePath atomically:YES];
NSString *filename = #"test.jpeg";
NSString *destDir = #"/";
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:nil fromPath:localPath];
I am an idiot, solved
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingString:#"test.jpg"];
NSData * data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data writeToFile:path atomically:YES];
[self.restClient uploadFile:#"test.jpg" toPath:#"/" withParentRev:nil fromPath:path];
you will have to call DBRestClient methods from the main thread or a thread that has a run loop. Otherwise the delegate methods will not be called.
what you will have to do is first alloc init yoir DBRestClient object then make it's delegate self and then you can easily upload your file.below is an example
NSString *destDir = #"/";
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
[restClient uploadFile:yourfilename toPath:destDir withParentRev:nil fromPath:sourcepath];
Happy Coding!!!!!!
You can't save into the bundle. You should use documents or cache folder:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
I have an NSMutableArray of custom objects called Clip. Each clip Object has some NSString properties (clipName,tcIn, tcOut, file path and so on..). How do I write an xml File on disk where each clip in the array is a node of the xml file and its properties are elements of the node?
Thanks in advance
Dario
I think this is what should do you:
//Create xml string
NSMutableString *xmlString = [[NSMutableString alloc] init];
//Add all the data to the string
[xmlString appendFormat:#"<clips>"];
for (Clip *c in clipsArray)
{
[xmlString appendFormat:#"\n\t<clip>"];
[xmlString appendFormat:#"\n\t\t<clipName>%#</clipName>", c.clipName];
[xmlString appendFormat:#"\n\t\t<tcIn>%#</tcIn>", c.tcIn];
...
[xmlString appendFormat:#"</clip>"];
}
[xmlString appendFormat:#"</clips>"];
//Create a variable to represent the filepath of the outputted file
//This is taken from some code which saves to an iPhone app's documents directory, it might not be ideal
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:#"output.xml"];
//Actually write the information
NSData *data = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil];
Is there a class that allows compressing data using Zlib, or is using zlib.dylib directly the only possibility I have?
NSData+Compression is an easy to use NSData category implementation.
NSData+Compression.h
NSData+Compression.m
Usage:
NSData* compressed = [myData zlibDeflate];
NSData* originalData = [compressed zlibInflate];
Here is what worked for me:
1) ZLib based Objective-Zip new location: https://github.com/gianlucabertani/Objective-Zip
Podfile:
pod 'objective-zip', '~> 1.0'
Quick example:
#import "ViewController.h"
#import "Objective-Zip.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:#"test.zip"]];
OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:path
mode:OZZipFileModeCreate];
NSString *str = #"Hello world";
OZZipWriteStream *stream= [zipFile writeFileInZipWithName:#"file.txt"
compressionLevel:OZZipCompressionLevelBest];
[stream writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
[stream finishedWriting];
[zipFile close];
}
2) Other zlib based library worked fine too. https://github.com/ZipArchive/ZipArchive
note: sometimes it's needed to add libz.tbd (new name of zlib.dylib) to "Link Binary With Libraries"
Quick example:
#import "SSZipArchive.h"
...
- (void)viewDidLoad {
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSError *error;
NSString *str = #"Hello world";
NSString *fileName = [docsDir stringByAppendingPathComponent:#"test.txt"];
BOOL succeed = [str writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (succeed){
NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:#"test.zip"]];
[SSZipArchive createZipFileAtPath:path withFilesAtPaths:#[fileName]];
}
}
As alternative, there is also objective-zip, which is, "a small Cocoa/Objective-C library that wraps ZLib and MiniZip in an object-oriented friendly way."
Writing a file into a ".zip" archive is simple as executing the following code:
ZipWriteStream *stream = [zipFile writeFileInZipWithName:#"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];
The library allows also to read the content of a ".zip" file, and to enumerate the files it contains.
Listing the content of a ".zip" file is done from code similar to the following one.
ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:#"test.zip" mode:ZipFileModeUnzip];
NSArray *infos = [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSLog(#"- %# %# %d (%d)", info.name, info.date, info.size, info.level);
// Locate the file in the zip
[unzipFile locateFileInZip:info.name];
// Expand the file in memory
ZipReadStream *read = [unzipFile readCurrentFileInZip];
NSMutableData *data = [[NSMutableData alloc] initWithLength:256];
int bytesRead = [read readDataWithBuffer:data];
[read finishedReading];
}
How would I rename a file, keeping the file in the same directory?
I have a string containing a full path to a file, and a string containing a the new filename (and no path), for example:
NSString *old_filepath = #"/Volumes/blah/myfilewithrubbishname.avi";
NSString *new_filename = #"My Correctly Named File.avi";
I know about NSFileManager's movePath:toPath:handler: method, but I cannot workout how to construct the new file's path..
Basically I'm looking for the equivalent to the following Python code:
>>> import os
>>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi"
>>> new_filename = "My Correctly Named File.avi"
>>> dirname = os.path.split(old_filepath)[0]
>>> new_filepath = os.path.join(dirname, new_filename)
>>> print new_filepath
/Volumes/blah/My Correctly Named File.avi
>>> os.rename(old_filepath, new_filepath)
NSFileManager and NSWorkspace both have file manipulation methods, but NSFileManager's - (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler is probably your best bet. Use NSString's path manipulation methods to get the file and folder names right. For example,
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
Both classes are explained pretty well in the docs, but leave a comment if there's anything you don't understand.
It's worth noting that moving a file to itself will fail. I had a method that replaced spaces with underscores and made the file name lowercase and renamed the file to the new name. Files with only one word in the name would fail the rename as the new name would be identical on a case-insensitive file system.
The way I resolved this was to do a two step rename, first renaming the file to a temporary name and then renaming it to the intended name.
Some pseudocode explaining this:
NSString *source = #"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS
The solution:
NSString *source = #"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *temp = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-temp", newName]];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:temp error:nil];
[[NSFileManager defaultManager] movePath:temp toPath:target error:nil];
I just wanted to make this easier to understand for a newbie. Here's all the code:
NSString *oldPath = #"/Users/brock/Desktop/OriginalFile.png";
NSString *newFilename = #"NewFileName.png";
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
NSLog( #"File renamed to %#", newFilename );
here's a more recent example for iOS, the NSFileManager method is a little different:
NSString *newFilename = [NSString stringWithFormat:#"%#.m4a", newRecording.title];
NSString *newPath = [[newRecording.localPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] moveItemAtPath:newRecording.localPath toPath:newPath error:nil];
For the icing on top, a category on NSFileManager:
#implementation NSFileManager (FileManipulations)
- (void)changeFileNamesInDirectory:(NSString *)directory changeBlock:(NSString * (^) (NSString *fileName))block
{
NSString *inputDirectory = directory;
NSFileManager *fileManager = [NSFileManager new];
NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:inputDirectory error:nil];
for (NSString *fileName in fileNames) {
NSString *newFileName = block(fileName);
NSString *oldPath = [NSString stringWithFormat:#"%#/%#", inputDirectory, oldFileName];
// move to temp path so case changes can happen
NSString *tempPath = [NSString stringWithFormat:#"%#-tempName", oldPath];
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
NSError *error = nil;
[fileManager moveItemAtPath:oldPath toPath:tempPath error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
return;
}
[fileManager moveItemAtPath:tempPath toPath:newPath error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
}
}
}
#end