unity 解压zip。(备用留坑)


using UnityEngine;

using System.Collections;

using System;

using System.IO;

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

 

public class MangeZip : MonoBehaviour

{

    string sourcesPath = "";

    string targetPath = "";

    string filename = "test.zip";

    ZipUtils zu;

 

 

    // Use this for initialization

    void Start()

    {

        sourcesPath = Application.dataPath + "/Zip";

        targetPath = Application.dataPath + "/CompressZip";

        Debug.Log("sourcesPaht is:" + sourcesPath + "   " + targetPath);

        StartCoroutine(StartUnZip());

    }

 

 

    IEnumerator StartUnZip()

    {

        string uri = "file:///" + sourcesPath + "/" + filename;

        WWW www = new WWW(uri);

        Debug.Log("uri is:" + uri);

        yield return www;

 

        if (www.error != null)

        {

            Debug.Log("loading is faile");

        }

 

        if (www.isDone)

        {

            Debug.Log("www is success");

        }

 

        //如果路径不存在 则创建

        if (!Directory.Exists(targetPath))

        {

            Directory.CreateDirectory(targetPath);

        }

        //把当前的压缩包写入到指定的路径下

        File.WriteAllBytes(targetPath + "/" + filename, www.bytes);

        //ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = System.Text.Encoding.UTF8.CodePage;

        StartCoroutine(UnzipWithPath(targetPath + "/" + filename));

    }

 

 

    private int totalCount;

    private int doneCount;

    private int indicatorStep = 1;

    public IEnumerator UnzipWithPath(string path)

    {

        Debug.Log("paht is:" + path);

        //这是根目录的路径

        string dirPath = path.Substring(0, path.LastIndexOf('/'));

        Debug.Log("dirpath is:" + dirPath);

        //ZipEntry:文件条目 就是该目录下所有的文件列表(也就是所有文件的路径)

        ZipEntry zip = null;

        //输入的所有的文件流都是存储在这里面的

        ZipInputStream zipInStream = null;

        //读取文件流到zipInputStream

        zipInStream = new ZipInputStream(File.OpenRead(path));

        //循环读取Zip目录下的所有文件

        while ((zip = zipInStream.GetNextEntry()) != null)

        {

            Debug.Log("name is:" + zip.Name + " zipStream " + zipInStream);

            UnzipFile(zip, zipInStream, dirPath);

            doneCount++;

            if (doneCount % indicatorStep == 0)

            {

                yield return new WaitForEndOfFrame();

            }

        }

        try

        {

            zipInStream.Close();

        }

        catch (Exception ex)

        {

            Debug.Log("UnZip Error");

            throw ex;

        }

 

        Debug.Log("解压完成:" + path);

    }

    static void UnzipFile(ZipEntry zip, ZipInputStream zipInStream, string dirPath)

    {

        try

        {

            //文件名不为空

            if (!string.IsNullOrEmpty(zip.Name))

            {

                string filePath = dirPath;

                filePath += ("/" + zip.Name);

 

                //如果是一个新的文件路径 这里需要创建这个文件路径

                if (IsDirectory(filePath))

                {

                    Debug.Log("Create  file paht " + filePath);

                    if (!Directory.Exists(filePath))

                    {

                        Directory.CreateDirectory(filePath);

                    }

                }

                else

                {

                    FileStream fs = null;

                    //当前文件夹下有该文件  删掉  重新创建

                    if (File.Exists(filePath))

                    {

                        File.Delete(filePath);

                    }

                    fs = File.Create(filePath);

                    int size = 2048;

                    byte[] data = new byte[2048];

                    //每次读取2MB  直到把这个内容读完

                    while (true)

                    {

                        size = zipInStream.Read(data, 0, data.Length);

                        //小于0, 也就读完了当前的流

                        if (size > 0)

                        {

                            fs.Write(data, 0, size);

                        }

                        else

                        {

                            break;

                        }

                    }

                    fs.Close();

                }

            }

        }

        catch (Exception e)

        {

            throw new Exception();

        }

    }

 

 

    /// 

    /// 判断是否是目录文件

    /// 

    /// 

    /// 

    static bool IsDirectory(string path)

    {

 

        if (path[path.Length - 1] == '/')

        {

            return true;

        }

        return false;

    }

}

 

你可能感兴趣的:(Unity,unityIO,unity代码记录,unity功能操作记录)