.Zip文件到NSData

时间:2014-05-22 04:59:31

标签: ios objective-c nsdata cfsocket

您好我遇到了zip文件的麻烦,事情是我通过CFSocket发送多个.zip文件,但是在将文件写回服务器时它已损坏或每次我点击它们多个zip文件出现大小不一样。我已经尝试过制作拉链并且它有效,我也将其传递给NSData,我也没有问题可以帮助我吗?

- (IBAction)zipButton:(id)sender {

    BOOL isDir=NO;

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSArray *subpaths;

    NSString *toCompress = @"/Departures/";
    NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];
    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"/Departures/textFiles.zip"];
    NSData   *zipData;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:pathToCompress isDirectory:&isDir] && isDir){

        subpaths = [fileManager subpathsAtPath:pathToCompress];
    } else if ([fileManager fileExistsAtPath:pathToCompress]) {

        subpaths = [NSArray arrayWithObject:pathToCompress];
    }

    ZipArchive *za = [[ZipArchive alloc] init];
    [za CreateZipFile2:zipFilePath];
    if (isDir) {

        for(NSString *path in subpaths){

            NSString *fullPath = [pathToCompress stringByAppendingPathComponent:path];

            if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){

                [za addFileToZip:fullPath newname:path];
                zipData=[NSData dataWithContentsOfFile:fullPath];

            }
        }
    } else {

        [za addFileToZip:pathToCompress newname:toCompress];

    }

}

我正在写回如下:

[zip writeToFile: videos.zip atomically:YES];

0 个答案:

没有答案