Quote:
Originally Posted by lbendlin
For what it's worth, here is the code to unpack the physical zip file into a NSData structure. Caveat: will only work correctly if the ZIP contains one file.
Code:
-(NSData*) UnzipFileToData {
NSMutableData* data = [[NSMutableData alloc] init];
int ret = unzGoToFirstFile( _unzFile );
unsigned char buffer[4096] = {0};
if( ret!=UNZ_OK )
{
[self OutputErrorMessage:@"Failed to go to first file"];
return data;
}
ret = unzOpenCurrentFile( _unzFile );
if( ret!=UNZ_OK )
{
[self OutputErrorMessage:@"Error opening file"];
return data;
}
// reading data and write to data object
int read = 1 ;
while( read > 0 )
{
read=unzReadCurrentFile(_unzFile, buffer, 4096);
if( read > 0 )
{
[data appendBytes:buffer length:read];
}
else if( read<0 )
{
[self OutputErrorMessage:@"Failed to reading zip file"];
break;
}
else
break;
}
unzCloseCurrentFile( _unzFile );
ret = unzGoToNextFile( _unzFile );
return data;
}
|
don't work
I added NSString parameter
-(NSData*) UnzipFileToData: (NSString*) unzipFile {
NSMutableData* data = [[NSMutableData alloc] init];
int ret = unzGoToFirstFile( unzipFile );
unsigned char buffer[4096] = {0};
...........
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
return;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent: @"file.zip"];
NSData *dataDataSet = [self UnzipFileToData:appFile];
Program received signal: “EXC_BAD_ACCESS”.
in line int ret = unzGoToFirstFile( unzipFile );