我无法使用DownloadDataAsync方法。唯一存在的选项是DownloadStringAsync方法。如何使用此方法下载zip文件。(我是windows phone 8 app开发的新手)
答案 0 :(得分:0)
我只想分享对我有用的解决方案。 我创建了一个web请求给了url并将gzip文件下载到了独立的存储文件中。现在下载后我创建了一个目标文件流,并使用GZipStream的WriteByte方法将压缩的gzip流文件从源文件存储到目标文件。现在我们得到未压缩的文件。
注意:-GZipStream可以从NuGet管理器添加到Visual Studio。
以下是我用来下载和提取GZip文件的代码段。
public async Task DownloadZipFile(Uri fileAdress,string fileName) { 尝试 {
WebRequest request = WebRequest.Create(fileAdress);
if (request != null)
{
WebResponse webResponse = await request.GetResponseAsync();
if (webResponse.ContentLength != 0)
{
using (Stream response = webResponse.GetResponseStream())
{
if (response.Length != 0)
{
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(fileName))
isolatedStorage.DeleteFile(fileName);
using (IsolatedStorageFileStream file = isolatedStorage.CreateFile(fileName))
{
const int BUFFER_SIZE = 100 * 1024;
byte[] buf = new byte[BUFFER_SIZE];
int bytesread;
while ((bytesread = await response.ReadAsync(buf, 0, BUFFER_SIZE)) > 0)
{
file.Write(buf, 0, bytesread);
}
file.Close();
FileStream sourceFileStream = File.OpenRead(file.Name);
FileStream destFileStream = File.Create(AppResources.OpenZipFileName);
GZipStream decompressingStream = new GZipStream(sourceFileStream, CompressionMode.Decompress);
int byteRead;
while ((byteRead = decompressingStream.ReadByte()) != -1)
{
destFileStream.WriteByte((byte)byteRead);
}
decompressingStream.Close();
sourceFileStream.Close();
destFileStream.Close();
PhoneApplicationService.Current.State["DestinationFilePath"] = destFileStream.Name;
}
}
FileDownload = true;
}
}
}
}
if (FileDownload == true)
{
return DownloadStatus.Ok;
}
else
{
return DownloadStatus.Other;
}
}
catch (Exception exc)
{
return DownloadStatus.Other;
}
}
答案 1 :(得分:-1)
您必须使用DownloadStrringAsync方法吗?否则你可以查看这些:
How to extract zipped file received from HttpWebResponse?
download and decompress a zip file in windows phone 8 application
Extract zip file from isolatedstorage
How to download a GZIP file from web to Windows Phone 7 and unzip the contents
答案 2 :(得分:-1)
要首先从网址下载zip文件,需要将zip文件存储到独立存储中,之后将其解压缩并根据需要读取文件。