iOS 输出输出I/O流操作数据

使用NSData处理数据

NSString *str = @"<1f8b0808 a1ca7659 0008>";
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSData *firstData = [data subdataWithRange:NSMakeRange(0, 10)];
NSLog(@"%d", [firstData.description isEqualToString:str]);
NSData *lastData = [data subdataWithRange:NSMakeRange(32, data.length - 32)];
NSString *pathstring = [VGSourcePath stringByAppendingString:@"/text.zip"];
[lastData writeToFile:[VGSourcePath stringByAppendingString:@"/text.zip"] options:NSDataWritingAtomic error:nil];
BOOL unarchive = [VGZipArchive unzipFileAtPath:pathstring toDestination:[pathstring stringByDeletingPathExtension] delegate:nil];

使用NSFileHandle处理数据

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"new" ofType:@"zip"];
NSFileHandle *readFileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *data = [readFileHandle readDataOfLength:10];
NSString *str = @"<1f8b0808 a1ca7659 0008>";
    if ([data.description isEqualToString:str]){
        [readFileHandle seekToFileOffset:32];
        NSData *msgData = [readFileHandle readDataToEndOfFile];
        [readFileHandle closeFile];
        NSString *pathstring = [VGSourcePath stringByAppendingString:@"/text1.zip"];
        NSFileHandle *writeFile = [NSFileHandle fileHandleForWritingAtPath:pathstring];
        [writeFile writeData:msgData];
        [writeFile closeFile];
        BOOL unarchive = [VGZipArchive unzipFileAtPath:pathstring toDestination:[pathstring stringByDeletingPathExtension] delegate:nil];
    }

使用NSStream处理数据

- (BOOL)copyAndCheckFile:(NSString*)srcPath dest:(NSString*)destPath {
    NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath:srcPath];
    [inputStream open];
    NSInteger offSet = 32;
    uint8_t read32 [offSet];
    NSInteger bytes = [inputStream read: read32 maxLength:offSet];
    if (bytes < offSet) {
        [inputStream close];
        return NO;
    }
    uint8_t zipAry[10];
    //1f8b0808 a1ca7659 0008
    zipAry[0] = (uint8_t)0x1f;
    zipAry[1] = (uint8_t)0x8b;
    zipAry[2] = (uint8_t)0x08;
    zipAry[3] = (uint8_t)0x08;
    zipAry[4] = (uint8_t)0xa1;
    zipAry[5] = (uint8_t)0xca;
    zipAry[6] = (uint8_t)0x76;
    zipAry[7] = (uint8_t)0x59;
    zipAry[8] = (uint8_t)0x00;
    zipAry[9] = (uint8_t)0x08;
    //NSLog(@"===> txt2:%s", &zipAry[0]);
    for (int i = 0;i < 10; i++) {
        if (zipAry[i] != read32[i]) {
            [inputStream close];
            return NO;
        }
    }
    NSOutputStream *outStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:NO];
    [outStream open];
    NSInteger maxLength = 1024*4;
    uint8_t readBuffer [maxLength];
    while (YES) {
        NSInteger bytesRead = [inputStream read: readBuffer maxLength:maxLength];
        if (bytesRead > 0) {
            [outStream write:readBuffer maxLength:bytesRead];
        }
        else{
            break;
        }
    }
    [inputStream close];
    [outStream close];
    return YES;
}
  • 共有用法
- (void)copyFile:(NSString*)srcPath offset:(int)offset dest:(NSString*)destPath{
    NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath:srcPath];
    NSOutputStream *outStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:NO];
    [inputStream open];
    [outStream open];
    NSInteger maxLength = 1024*4;
    uint8_t readBuffer [maxLength];
    int pos = 0;
    while (YES) {
        NSInteger bytesRead = [inputStream read: readBuffer maxLength:maxLength];
        if (bytesRead > 0) {
            if (offset > 0 && pos < offset) {
                if (pos + bytesRead <= offset) {
                    pos += bytesRead;
                    continue;
                }
                else{
                    int left = offset - pos;
                    uint8_t readBuffer2 [bytesRead - left];
                    for (int i = 0; i < bytesRead - left; i++) {
                        readBuffer2[i] = readBuffer[i + left];
                    }
                    [outStream write:readBuffer2 maxLength:bytesRead - left];
                    pos += bytesRead;
                }
            }
            else{
                [outStream write:readBuffer maxLength:bytesRead];
            }
        }
        else{
            break;
        }
    }
    [inputStream close];
    [outStream close];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值