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];}
阅读全文
0 0
- iOS 输出输出I/O流操作数据
- 输入、输出流以及文件操作(I/O、File)
- I/O输入、输出
- JavaSE I/O 输出字节流 OutputStream
- 如何正确使用Java I/O输出和读入数据
- 如何正确使用Java I/O输出和读入数据
- 如何正确使用Java I/O输出和读入数据
- 如何正确使用Java I/O输出和读入数据
- 如何正确使用Java I/O输出和读入数据
- 如何正确使用Java I/O输出和读入数据
- C++ 输入/输出(I/O)
- Java I/O(输入/输出)
- I/O(输入/输出)
- I/O流操作中,如何区分输入流和输出流呢?
- 厚积薄发JAVA文件操作--java I/O中通过字符流读取文件并输出
- java 输入/输出流(I/O流)
- Unix标准输入/输出 (I/O) 流知识讲解
- 黑马程序员——I/O(输入/输出)流
- Struts基础案例
- GIT团队合作探讨之二--Pull Request
- 为何String对象是immutable或final的?
- GIT团队合作探讨之三--使用分支
- 三表左连接
- iOS 输出输出I/O流操作数据
- 集合介绍 简单明了
- 数据库系统
- GIT团队合作探讨之四--不同工作流优缺辨析
- git rebase vs git merge详解
- 翻译:AVAssetReader
- 如何保持自己的应用和关联的第三方框架同步更新?
- 凯丽提供的几个Linux命令,总结一下
- git rebase --onto详解