使用HttpWebRequest下载Zip文件并解压到相应文件夹

使用HttpWebRequest下载Zip文件并解压到相应文件夹,第1张

string persistentDataPath_ = Application.persistentDataPath

string resourcePath_ = Path.Combine(persistentDataPath_, "151515")

//设置存储位置

string zipPath_ = (resourcePath_ + ".zip").Replace(@"\", "/")

if (Directory.Exists(resourcePath_))

            Directory.Delete(resourcePath_, true)

if (File.Exists(zipPath_))

            File.Delete(zipPath_)

String filePath = @"您的url地址"//本地路径不行,必须是http或者https或者www才可以

//写入流

FileStream stream = File.Open(zipPath_, FileMode.OpenOrCreate)

//创建httpwebrequest请求

HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create(filePath)

downloadRequest.Method = "GET"

using (HttpTools downloader_ = new HttpTools(downloadRequest, null, stream))

{

            yield return downloader_

            bool updateResult = false

            //清空数据流关闭

       芹袜     stream.Flush()

            stream.Close()

            bool decompressFinish_ = false

            if (downloader_.Exception == null)

            {

                // 解压资源包

                AsyncExtra(zipPath_, persistentDataPath_, delegate(bool b)

                {

                    updateResult = b

                    decompressFinish_ = true

                })

     橘则           // 等待压缩完成

                while (!decompressFinish_) yield return null

            }

            else

            {

                //ReportManager.Inst.ReportException(this.downloader_.Exception)

                decompressFinish_ = true

           圆首棚 }

            if (updateResult)

            {

                LoadPrint("更新完成")

            }

            else

            {

                LoadPrint("更新失败")

            }

}

//第二部分

/// <summary>

/// 将指定的zip文件解压缩到指定目录

/// </summary>

/// <param name="path_"></param>

/// <param name="targetDir"></param>

void AsyncExtra(string path_, string targetDir, Action<bool>cb)

{

    Thread t = new Thread(delegate()

    {

           try

           {

                if (!Directory.Exists(targetDir) &&!string.IsNullOrEmpty(targetDir))

                Directory.CreateDirectory(targetDir)

                using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(path_)))

                {

                    //this.state_ = ResourceUpdateState.Decompression

                    ZipEntry entry

                    while ((entry = zipStream.GetNextEntry()) != null)

                        ExtraZipEntry(zipStream, entry, targetDir)

                }

                cb(true)

            }

            catch (Exception e)

            {

                //ReportManager.Inst.ReportException(e)

                cb(false)

            }

        })

        t.Start()

}

//第三部分

void ExtraZipEntry(ZipInputStream zipStream, ZipEntry entry, string outputDir)

    {

        int bufferSize_ = 8192

        byte[] buffer = new byte[bufferSize_]

        int readSize_ = 0

        string directroyName = Path.GetDirectoryName(entry.Name)

        string fileName = Path.GetFileName(entry.Name)

        string absDirectory = Path.Combine(outputDir, directroyName)

        string absFile = Path.Combine(absDirectory, fileName)

        Debug.Log("解压信息:" + directroyName + "  " + fileName + "  " + absDirectory + "  " + absFile)

        if (!string.IsNullOrEmpty(directroyName))

            Directory.CreateDirectory(absDirectory)

        int size = (int)entry.Size

        using (FileStream fileStream = File.Create(absFile))

        {

            while (readSize_ <size)

            {

                int rd = zipStream.Read(buffer, 0, Math.Min(size - readSize_, bufferSize_))

                fileStream.Write(buffer, 0, rd)

                readSize_ += rd

            }

        }

    }

1、上网搜索并下载应用程序“WINRAR”。下载完成后直接进行安装,其安装过程很简单。

2、安装完成后,在需要打开的zip文件上右击,从d慧扰梁出的菜单中选择以“打开方式”,在d出的“打开方式”窗口中选择“WINRAR压缩文件管理器",然后点击“确定"按钮。.

3、然后就会发现zip文件可以被打开啦。

4、接着选择要 *** 作的文前运件或文件夹,点击主界面中的”解压缩“按钮,在d出的窗口中选择要保存的文件路径即可。

5、还可以直接在需要打开的压李羡缩文件上右击,从d出的菜单中选择“WINRAR”→“解压到feifeidown\”。则会自动创建一个名称与压缩文件名相同的文件夹。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12247499.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存