내 번들에 파일 압축을 풀고 쓰기를 원하는 두 개의 .tgz 파일이있다. 나는 그것을 작동 시켰어. 일종의. 문제는 작성된 파일이 앞에 512 바이트의 쓰레기 데이터를 가져 오지만 그 이외의 파일은 압축되지 않은 것입니다.zlib iPhone - 파일이 허튼 소리를 낸다
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];
tgz에서 추가 한 헤더 정보 일 수 있습니까? – Jasarien