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

来源:互联网 发布:算法设计与分析试题 编辑:程序博客网 时间:2024/06/06 08:40

使用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];}
原创粉丝点击