qua*_*ano 2 compression iphone zlib
我的包中有几个.tgz文件,我想要解压缩并写入文件.我有它的工作 - 有点.问题是写入的文件前面有512字节的垃圾数据,但除此之外,该文件已成功解压缩.
alt text http://pici.se/pictures/acbdwGCvX.gif
我不想要废话.如果它总是512字节,那么当然很容易跳过那些并写下其他的.但它总是这样吗?如果不知道为什么那些字节在那里,那么冒险做类似的事情.
gzFile f = gzopen ([[[NSBundle mainBundle] pathForResource:file ofType:@"tgz"] cStringUsingEncoding:NSASCIIStringEncoding], [@"rb" cStringUsingEncoding:NSASCIIStringEncoding]);
unsigned int length = 1024*1024;
void *buffer = malloc(length);
NSMutableData *data = [NSMutableData new];
while (true)
{
int read = gzread(f, buffer, length);
if (read > 0)
{
[data appendBytes:buffer length:read];
}
else if (read == 0)
break;
else if (read == -1)
{
throw [NSException exceptionWithName:@"Decompression failed" reason:@"read = -1" userInfo:nil];
}
else
{
throw [NSException exceptionWithName:@"Unexpected state from zlib" reason:@"read < -1" userInfo:nil];
}
}
int writeSucceeded = [data writeToFile:file automatically:YES];
free(buffer);
[data release];
if (!writeSucceeded)
throw [NSException exceptionWithName:@"Write failed" reason:@"writeSucceeded != true" userInfo:nil];
Run Code Online (Sandbox Code Playgroud)
根据您发布的代码,您似乎只是尝试使用gzip读取Tar'ed gZip的文件.
我的猜测是解压后文件开头的"垃圾"实际上是TAR文件头(我在开始时看到一个文件名).
Tar File Format的更多提示指向512字节大小.
gzip只能压缩单个文件.如果您只是尝试压缩单个文件,则不需要先对其进行tar.
如果您尝试压缩多个文件并作为单个存档,则需要使用TAR并在解压缩后解压缩文件.
只是一个猜测.
克里斯.
| 归档时间: |
|
| 查看次数: |
796 次 |
| 最近记录: |