2013-03-01 75 views

回答

0

我只是想分享的是對我工作的解決方案。 我創建了一個web請求到給定的url並將gzip文件下載到獨立的存儲文件中。現在下載後我創建了一個目標文件流並使用GZipStream的WriteByte方法將源文件中的壓縮gzip流文件存儲到目標文件。獲取未壓縮的文件。

注意:-GZipStream可以從NuGet管理器添加到Visual Studio。

這是我用來下載和提取GZip文件的代碼片段。

公共異步任務DownloadZipFile(URI fileAdress,字符串文件名){ 嘗試 {

  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; 
     } 

    }