Compress CSV file with GZIP in xcode - ios

I need to create a big table to csv file and compress it.
I can create the csv file like this: Xcode - Create csv/spreadsheet file
It is possible compress it with GZIP?
Thanks for all.
Best regards.

I recommend you use SSZipArchive: https://github.com/soffes/ssziparchive
Then you can do:
NSString *content = #"1,2,3"
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:#"somefile.zip"];
SSZipArchive *zip = [[SSZipArchive alloc] initWithPath:path];
[zip open];
[zip writeData:[content dataUsingEncoding:NSUTF8StringEncoding] filename:#"data.csv"];
[zip close];
That will produce a zip file named "somefile.zip" in the temporary directory, containing one file inside named "data.csv"

The easiest way is to probably not use a standard copy build phase to move the file into place, but instead use shell script build phase to copy the file and then compress it in place with gzip.

Related

Access any file inside Zip File [duplicate]

I'm trying to port an existing Android application to iOS,
In the Android application i was using a ZipInputStream to extract a single file from the zip archive and store it in as a temporary file.
How could i extract a single file from a Zip Archive without having to extract the whole archive (As it is very large)?
I just found the answer,
I had to modify SSZipArchive to insert a method that will extract a single file from the ZIP archive (Based on its name)
You could find the modified version here, if someone finds this useful i might clean it up, add the delegate, the tests and propose for pull in SSZipArchive.
Usage is straightforward:
NSString *zipEntityToExtract = #"example.aac";
NSString *destinationFilePath = ...; //(Includes filename)
NSString *zipPath = ...;
[SSZipArchive unzipEntityName:zipEntityToExtract fromFilePath:zipPath toDestination:filePath];
//File is now in destinationFilePath

Unzipping a single file from Archive

I'm trying to port an existing Android application to iOS,
In the Android application i was using a ZipInputStream to extract a single file from the zip archive and store it in as a temporary file.
How could i extract a single file from a Zip Archive without having to extract the whole archive (As it is very large)?
I just found the answer,
I had to modify SSZipArchive to insert a method that will extract a single file from the ZIP archive (Based on its name)
You could find the modified version here, if someone finds this useful i might clean it up, add the delegate, the tests and propose for pull in SSZipArchive.
Usage is straightforward:
NSString *zipEntityToExtract = #"example.aac";
NSString *destinationFilePath = ...; //(Includes filename)
NSString *zipPath = ...;
[SSZipArchive unzipEntityName:zipEntityToExtract fromFilePath:zipPath toDestination:filePath];
//File is now in destinationFilePath

Load Pods-acknowledgements.markdown as NSString

I'm building an application and want to load the cocoapods auto-generated acknowledgements markdown file into an NSString to be displayed in my application. I though it would be as simple, as doing this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Pods-acknowledgements" ofType:#"markdown"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
But this doesn't work.... Is there a way to access this file?
It doesn't look like this file is automatically copied into your project bundle.
You need to add this file to your Copy bundle Resource build phase. It's also worth nothing that the file name actually contains your project's name Pods-<PROJECT_NAME>-acknowledgements
In practice you may want to make a symbolic link in your project that points to the generated file in the Pods directory to ensure that the newly generated file is used each time

Objective-Zip: Store content when unzip file (iOS)

I just want to create a file with the data of unzipped file in iOS.
I am using the Objective-Zip library, and I can zip files rightly.
Now, I am trying to extract the files of this zip file.
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];
//Checking files inside zip
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos)
NSLog(#"Test 1: - %# %# %d (%d)", info.name, info.date, info.size, info.level);
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZipWithPassword:password];
How I can pass the ZipReadStream to a NSData object? Then I would convert NSData to the right file, is not?
Thanks in advance.
Sorry, it seems to be duplicate: unzip NSData using objective zip
Looking at the headers for the ZipReadStream class, it looks like there is a method readDataOfLength. It appears that you call that method repeatedly until you have all the data from the file. I would expect a method to find out the length of the unzipped file, but did not see such a method from a quick glance.
As a side-note, I've never heard of the Objective-Zip library until a few minutes ago, and I found the 'readDataOfLength:' method in less than a minute. Perhaps you should spend some time looking through the library's header and sample code?

iOS Objective-Zip corruption issues

I am using flyingdolphinstudio's Objective-Zip which is an Objective-C zipping library. I am using this to zip a .txt file following the answer given in this question.
For my case, I do not have an array to cycle through so just for a single file I have the following:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:zip_file_name mode:ZipFileModeCreate];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:txt_file_name error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
NSData *filedata = [NSData dataWithContentsOfFile:txt_file_name];
ZipWriteStream *stream = [zipFile writeFileInZipWithName:txt_file_name fileDate:Date compressionLevel:ZipCompressionLevelBest];
[stream writeData:filedata];
[stream finishedWriting];
[zipFile close];
The zipping process seems to work, I get a .zip file with the correct name and a non-zero size. However, when I try and un-zip this on my mac, it runs into a .cpgz loop. And judging by this article, I presume its because my file is getting corrupted somewhere in there. Also when I upload it to a server and we try to open it on a linux machine, it spits it back out at us with the error:
$ unzip COCR2_100.zip
Archive: COCR2_100.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
note: COCR2_100.zip may be a plain executable, not an archive
unzip: cannot find zipfile directory in one of COCR2_100.zip or
COCR2_100.zip.zip, and cannot find COCR2_100.zip.ZIP, period.
Which also suggests it is corrupted.
Does anybody have any ideas as to why it may be getting corrupted?
Thanks!
Solved this by switching to a different Zip client. Moved from Objective-Zip to ZipArchive and that seemed to work just fine.

Resources