Objective-Zip: Store content when unzip file (iOS) - 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?

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

Objective Zip archives aren't opened by ArchiveUtility.app

I'm using ObjectiveZip library (which is a wrapper for MiniZip) in my iOS app. My app sends .zip archives to server, where they are processed manually by moderator. Here's my code for creating an archive:
NSString * zipfile = [Result zipfilePathWithResult:self];
ZipFile * zf = [[ZipFile alloc] initWithFileName:zipfile mode:ZipFileModeCreate];
ZipWriteStream * zws = [zf writeFileInZipWithName:#"report.xml" compressionLevel:ZipCompressionLevelNone];
[zws writeData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
[zws finishedWriting];
for (NSString * name in mediaFiles)
{
ZipWriteStream * zws = [zf writeFileInZipWithName:name compressionLevel:ZipCompressionLevelNone];
[zws writeData:[files objectForKey:name]];
[zws finishedWriting];
}
[zf close];
Unfortunately, these archives aren't correctly processed by default OS X ArchiveUtility: it always unarchives my files into zip.cpgz, regardless of the actual content of archive (usually it's a .xml file and a few .jpg files).
However, some other applications on OS X and also on Windows are able to open my archives correctly, so the archive isn't actually broken.
Is there a way to make ObjectiveZip work with ArchiveUtility? Or maybe you can suggest any other objective-c library for creating .zip files which can do it. Thanks in advance!
Edit zip.c changing the parameters passed into each calls to zipOpenNewFileInZip3_64. The last parameter being passed into each method is a 1. Change it to a 0 and it will work
Changing the last parameter to zipOpenNewFileInZipX_64 function is one way to fix this but changing this parameter to 0 means setting zip64, i.e large file zipping capability, to false.
I was able to fix this by setting second parameter of zip64local_putValue call to (uLong)20(which is currently 45 in one case) inside the function Write_LocalFileHeader regardless of value of zi->ci.zip64.

How to convert Excel file in csv and read data from iphone

I want to make an application that read excel file from url and convert data in to csv file and then i can read the csv file and display the data. how can i do this. please help me.
If you have a .xls file, you can use the open source DHlibxls library to read the file into your app. This is an ObjectiveC framework that wraps a C-based library. The library is quite mature.
How to convert Excel file in csv
Click here
and read data from iphone
//******** Get Data fron CSV file  **********//
NSString *path = [[NSBundle mainBundle] pathForResource:#"NameYourCsvFile" ofType:#"csv"];
NSError* error;
dataStr=[NSString stringWithContentsOfFile:path encoding:NSStringEncodingConversionAllowLossy error:&error ];

how to zip empty folders in File manager in iOS?

I am developing a app like file manager. In this i have created zip folder using any existing folder, it works perfectly fine. For this i have used SSZipArchive API. After zip i send it through mail. It's working fine too.
Now when i try to zip any empty folder, it is not working. Means empty folder does not zip.
Yeah looking at the source code, the method + createZipFileAtPath:withContentsOfDirectory: only appears to care about files:
while ((fileName = [dirEnumerator nextObject])) {
BOOL isDir;
NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:fileName];
[fileManager fileExistsAtPath:fullFilePath isDirectory:&isDir];
if (!isDir) {
[zipArchive writeFileAtPath:fullFilePath withFileName:fileName];
}
}
You have two choices:
Fix it yourself and then send a pull request to the author on github and contribute to work you have, yourself, benefited from.
Live with it. It's not clear how an empty folder not appearing in the zip file is an issue anyway.
Isn't a standard thing to have stub files in directories like that. Some file with no contents.

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