将.zip文件转换为NSData [英] convert .zip file into NSData

查看:1094
本文介绍了将.zip文件转换为NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,用zip文件初始化NSData是正确的吗?我想将一个zip文件转换为NSData并构造另一个文件与数据(在简单的语言复制它)。我有如下代码:

Hey, Is it correct to initialize an NSData with a zip file? I want to convert a zip file into NSData and construct another file with the data (in simple language 'copy it'). I have the code as:

NSURL *theFileUrl = [NSURL URLWithString: @"file://localhost/Users/xxx/Desktop/testZippedFile.zip"];

NSData *data = [NSData dataWithContentsOfURL: theFileUrl];

当我, NSLog(@Data:%@,data) ,我得到一些输出,但是当我尝试初始化NSString与这些数据,它不工作:

When I, NSLog(@"Data: %@", data) , i do get some output but when I try to initialize an NSString with this data, it doesn't work:

 NSString *str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
`NSLog(@"String: %@", string)`

log as: String:PK

任何人都可以指出我的错误。
提前感谢!

Can anyone point out my mistakes please. Thanks in advance!

推荐答案

为什么会这样? NSFileManager 将为你做的:)

Why do it that way? NSFileManager will do it for you :)

[[NSFileManager defaultManager] copyItemAtPath:oldPath toPath:newPath error:nil];






但是,这只适用于本地 - 如果您要从服务器复制文件,请查看 NSURLConnection 加载数据,然后NSData的writeToFile:atomically:方法将内容保存到文件系统(在这里。)


However, this only works for files that are local - if you want to copy a file from a server, you should have a look at NSURLConnection to load the data and then NSData's writeToFile:atomically: method to save the contents to the file system (found here.)

这篇关于将.zip文件转换为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆