// Use this for initialization
 void Start()
 {
     cp   = new CodeProgress(SetProgress);
     path = Application.dataPath;
     //path = System.Environment.CurrentDirectory;
     //path = path.Replace('\\','/')+"/Assets";
 }
コード例 #2
0
        /**  同步压缩一个文件  **/
        private static void Compress(object obj)
        {
            FileChangeInfo info         = (FileChangeInfo)obj;
            string         inpath       = info.inpath;
            string         outpath      = info.outpath;
            CodeProgress   codeProgress = null;

            if (info.progressDelegate != null)
            {
                codeProgress = new CodeProgress(info.progressDelegate);
            }

            try
            {
                SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
                FileStream inputFS  = new FileStream(inpath, FileMode.Open);
                FileStream outputFS = new FileStream(outpath, FileMode.Create);

                encoder.WriteCoderProperties(outputFS);

                outputFS.Write(System.BitConverter.GetBytes(inputFS.Length), 0, 8);

                encoder.Code(inputFS, outputFS, inputFS.Length, -1, codeProgress);
                outputFS.Flush();
                outputFS.Close();
                inputFS.Close();
                Debug.Log("压缩完毕");
            }
            catch (Exception ex)
            {
                Debug.Log(ex);
            }
        }
コード例 #3
0
ファイル: LZMAFile.cs プロジェクト: hanbim520/Unity3D-7Zip
        /**  同步解压一个文件  **/
        private static void DeCompress(object obj)
        {
            FileChangeInfo info         = (FileChangeInfo)obj;
            string         inpath       = info.inpath;
            string         outpath      = info.outpath;
            CodeProgress   codeProgress = null;

            codeProgress = new CodeProgress(info.progressPercentDelegate, info.progressDelegate);

            try
            {
                SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                FileStream inputFS  = new FileStream(inpath, FileMode.Open);
                FileStream outputFS = new FileStream(outpath, FileMode.Create);

                int    propertiesSize = 5;
                byte[] properties     = new byte[propertiesSize];
                inputFS.Read(properties, 0, properties.Length);

                byte[] fileLengthBytes = new byte[8];
                inputFS.Read(fileLengthBytes, 0, 8);
                long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0);

                decoder.SetDecoderProperties(properties);
                decoder.Code(inputFS, outputFS, inputFS.Length, fileLength, codeProgress);
                outputFS.Flush();
                outputFS.Close();
                inputFS.Close();
                Debug.Log("解压完毕");
            }
            catch (Exception ex)
            {
                Debug.Log(ex);
            }
        }
コード例 #4
0
        /**  同步解压一个文件  **/
        private static void DeCompress(object obj)
        {
            FileChangeInfo info         = (FileChangeInfo)obj;
            string         inpath       = info.inpath;
            string         outpath      = info.outpath;
            CodeProgress   codeProgress = null;

            if (info.progressDelegate != null)
            {
                codeProgress = new CodeProgress(info.progressDelegate);
            }

            ICSharpCode.SharpZipLib.GZip.GZip.Decompress(File.OpenRead(inpath), File.Create(outpath), true, codeProgress);
        }
コード例 #5
0
ファイル: LZMAHelper.cs プロジェクト: syjsu/LZMA-with-Unity3D
    public static void Compress(string inpath, string outpath, CodeProgress progress)
    {
        SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
        FileStream inputFS  = new FileStream(inpath, FileMode.Open);
        FileStream outputFS = new FileStream(outpath, FileMode.Create);

        encoder.WriteCoderProperties(outputFS);

        outputFS.Write(System.BitConverter.GetBytes(inputFS.Length), 0, 8);

        encoder.Code(inputFS, outputFS, inputFS.Length, -1, progress);
        outputFS.Flush();
        outputFS.Close();
        inputFS.Close();
    }
コード例 #6
0
ファイル: LZMAHelper.cs プロジェクト: syjsu/LZMA-with-Unity3D
    public static void DeCompress(string inpath, string outpath, CodeProgress progress)
    {
        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
        FileStream inputFS  = new FileStream(inpath, FileMode.Open);
        FileStream outputFS = new FileStream(outpath, FileMode.Create);

        int propertiesSize = SevenZip.Compression.LZMA.Encoder.kPropSize;

        byte[] properties = new byte[propertiesSize];
        inputFS.Read(properties, 0, properties.Length);

        byte[] fileLengthBytes = new byte[8];
        inputFS.Read(fileLengthBytes, 0, 8);
        long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0);

        decoder.SetDecoderProperties(properties);
        decoder.Code(inputFS, outputFS, inputFS.Length, fileLength, progress);
        outputFS.Flush();
        outputFS.Close();
        inputFS.Close();
    }
コード例 #7
0
ファイル: AppEntrance.cs プロジェクト: fujisheng/EscapeDemo
 private void Start()
 {
     progress = new CodeProgress(
         LanguageManager.GetInstance().Init,
         AudioManager.GetInstance().Init,
         StoreManager.GetInstance().Init,
         IAPManager.GetInstance().Init,
         UrlManager.GetInstance().Init,
         LocalAdManager.GetInstance().Init,
         ADManager.GetInstance().Init,
         TipsManager.GetInstance().Init,
         InventoryManager.GetInstance().Init,
         PlotManager.GetInstance().Init,
         MoreGameManager.GetInstance().Init,
         FeedbackManager.GetInstance().Init,
         ShareManager.GetInstance().Init,
         DataManager.GetInstance().Init,
         DataManager.GetInstance().LoadFile
         );
     progress.onProgress += OnProgress;
     StartCoroutine(Init());
 }
コード例 #8
0
    void Start()
    {
        ApplicationdataPath = Application.dataPath;

        m_CodeProgress = new CodeProgress(SetProgressPercent);
    }
コード例 #9
0
ファイル: UpkExtra.cs プロジェクト: syjsu/LZMA-with-Unity3D
    public static void ExtraUPK(string upkfilepath, string outputpath, CodeProgress progress)
    {
        int totalsize = 0;

        FileStream upkFilestream = new FileStream(upkfilepath, FileMode.Open);

        upkFilestream.Seek(0, SeekOrigin.Begin);

        int offset = 0;

        //读取文件数量;
        byte[] totaliddata = new byte[4];
        upkFilestream.Read(totaliddata, 0, 4);
        int filecount = BitConverter.ToInt32(totaliddata, 0);

        offset += 4;
        Debug.Log("filecount=" + filecount);

        //读取所有文件信息;
        for (int index = 0; index < filecount; index++)
        {
            //读取id;
            byte[] iddata = new byte[4];
            upkFilestream.Seek(offset, SeekOrigin.Begin);
            upkFilestream.Read(iddata, 0, 4);
            int id = BitConverter.ToInt32(iddata, 0);
            offset += 4;

            //读取StartPos;
            byte[] startposdata = new byte[4];
            upkFilestream.Seek(offset, SeekOrigin.Begin);
            upkFilestream.Read(startposdata, 0, 4);
            int startpos = BitConverter.ToInt32(startposdata, 0);
            offset += 4;

            //读取size;
            byte[] sizedata = new byte[4];
            upkFilestream.Seek(offset, SeekOrigin.Begin);
            upkFilestream.Read(sizedata, 0, 4);
            int size = BitConverter.ToInt32(sizedata, 0);
            offset += 4;

            //读取pathLength;
            byte[] pathLengthdata = new byte[4];
            upkFilestream.Seek(offset, SeekOrigin.Begin);
            upkFilestream.Read(pathLengthdata, 0, 4);
            int pathLength = BitConverter.ToInt32(pathLengthdata, 0);
            offset += 4;

            //读取path;
            byte[] pathdata = new byte[pathLength];
            upkFilestream.Seek(offset, SeekOrigin.Begin);
            upkFilestream.Read(pathdata, 0, pathLength);
            string path = m_UTF8Encoding.GetString(pathdata);
            offset += pathLength;


            //添加到Dic;
            OneFileInfor info = new OneFileInfor();
            info.m_id         = id;
            info.m_Size       = size;
            info.m_PathLength = pathLength;
            info.m_Path       = path;
            info.m_StartPos   = startpos;
            m_allFileInfoDic.Add(id, info);

            totalsize += size;

            Debug.Log("id=" + id + " startPos=" + startpos + " size=" + size + " pathLength=" + pathLength + " path=" + path);
        }



        //释放文件;
        int totalprocesssize = 0;

        foreach (var infopair in m_allFileInfoDic)
        {
            OneFileInfor info = infopair.Value;

            int    startPos = info.m_StartPos;
            int    size     = info.m_Size;
            string path     = info.m_Path;

            //创建文件;
            string dirpath  = outputpath + path.Substring(0, path.LastIndexOf('/'));
            string filepath = outputpath + path;
            if (Directory.Exists(dirpath) == false)
            {
                Directory.CreateDirectory(dirpath);
            }
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }

            FileStream fileStream = new FileStream(filepath, FileMode.Create);

            byte[] tmpfiledata;
            int    processSize = 0;
            while (processSize < size)
            {
                if (size - processSize < 1024)
                {
                    tmpfiledata = new byte[size - processSize];
                }
                else
                {
                    tmpfiledata = new byte[1024];
                }

                //读取;
                upkFilestream.Seek(startPos + processSize, SeekOrigin.Begin);
                upkFilestream.Read(tmpfiledata, 0, tmpfiledata.Length);

                //写入;
                fileStream.Write(tmpfiledata, 0, tmpfiledata.Length);

                processSize      += tmpfiledata.Length;
                totalprocesssize += tmpfiledata.Length;

                progress.SetProgressPercent((long)totalsize, (long)totalprocesssize);
            }
            fileStream.Flush();
            fileStream.Close();
        }
    }
コード例 #10
0
        /**  同步打包一个文件夹  **/
        private static void PackFolder(object obj)
        {
            FileChangeInfo pathinfo = (FileChangeInfo)obj;
            string         inpath   = pathinfo.inpath;
            string         outpath  = pathinfo.outpath;
            CodeProgress   progress = null;

            if (pathinfo.progressDelegate != null)
            {
                progress = new CodeProgress(pathinfo.progressDelegate);
            }

            int id        = 0;
            int totalSize = 0;
            Dictionary <int, OneFileInfor> allFileInfoDic = new Dictionary <int, OneFileInfor>();

            /**  遍历一个文件夹的所有文件  **/

            Debug.Log("遍历文件夹 " + inpath);

            string sourceDirpath = inpath.Substring(0, inpath.LastIndexOf('/'));

            /** 读取文件夹下面所有文件的信息 **/
            DirectoryInfo dirInfo = new DirectoryInfo(inpath); //创建子目录

            foreach (FileInfo fileinfo in dirInfo.GetFiles("*.*", SearchOption.AllDirectories))
            {
                //如果拓展名为.meta表示为unity为每个资源生成的标识文件.
                if (fileinfo.Extension == ".meta")
                {
                    continue;
                }

                //规范化相对路径
                string filename = fileinfo.FullName.Replace("\\", "/");
                filename = filename.Replace(sourceDirpath + "/", "");

                int filesize = (int)fileinfo.Length;

                Debug.Log(id + " : " + filename + " 文件大小: " + filesize);

                OneFileInfor info = new OneFileInfor();
                info.id         = id;
                info.size       = filesize;
                info.path       = filename;
                info.pathLength = new UTF8Encoding().GetBytes(filename).Length;

                /**  读取这个文件  **/
                FileStream fileStreamRead = new FileStream(fileinfo.FullName, FileMode.Open, FileAccess.Read);
                if (fileStreamRead == null)
                {
                    Debug.Log("读取文件失败 : " + fileinfo.FullName);
                    return;
                }
                else
                {
                    byte[] filedata = new byte[filesize];
                    fileStreamRead.Read(filedata, 0, filesize);
                    info.data = filedata;
                }
                fileStreamRead.Close();


                allFileInfoDic.Add(id, info);

                id++;
                totalSize += filesize;
            }

            /**  遍历一个文件夹的所有文件 结束  **/

            Debug.Log("文件数量 : " + id);
            Debug.Log("文件总大小 : " + totalSize);

            /**  UPK中前面是写每个包的ID,StartPos,size,pathLength,path.
             * /**  更新文件在UPK中的起始点  **/
            int firstfilestartpos = 0 + 4;

            for (int index = 0; index < allFileInfoDic.Count; index++)
            {
                firstfilestartpos += 4 + 4 + 4 + 4 + allFileInfoDic[index].pathLength;
            }

            int startpos = 0;

            for (int index = 0; index < allFileInfoDic.Count; index++)
            {
                if (index == 0)
                {
                    startpos = firstfilestartpos;
                }
                else
                {
                    startpos = allFileInfoDic[index - 1].startPos + allFileInfoDic[index - 1].size;//上一个文件的开始+文件大小;
                }

                allFileInfoDic[index].startPos = startpos;
            }

            /**  写文件  **/
            FileStream fileStream = new FileStream(outpath, FileMode.Create);

            /**  文件总数量  **/
            byte[] totaliddata = System.BitConverter.GetBytes(id);
            fileStream.Write(totaliddata, 0, totaliddata.Length);

            for (int index = 0; index < allFileInfoDic.Count; index++)
            {
                /** 写入ID **/
                byte[] iddata = System.BitConverter.GetBytes(allFileInfoDic[index].id);
                fileStream.Write(iddata, 0, iddata.Length);

                /**  写入StartPos  **/
                byte[] startposdata = System.BitConverter.GetBytes(allFileInfoDic[index].startPos);
                fileStream.Write(startposdata, 0, startposdata.Length);

                /**  写入size  **/
                byte[] sizedata = System.BitConverter.GetBytes(allFileInfoDic[index].size);
                fileStream.Write(sizedata, 0, sizedata.Length);

                /**  写入pathLength  **/
                byte[] pathLengthdata = System.BitConverter.GetBytes(allFileInfoDic[index].pathLength);
                fileStream.Write(pathLengthdata, 0, pathLengthdata.Length);

                /**  写入path  **/
                byte[] mypathdata = new UTF8Encoding().GetBytes(allFileInfoDic[index].path);

                fileStream.Write(mypathdata, 0, mypathdata.Length);
            }

            /**  写入文件数据  **/
            int totalprocessSize = 0;

            foreach (var infopair in allFileInfoDic)
            {
                OneFileInfor info        = infopair.Value;
                int          size        = info.size;
                byte[]       tmpdata     = null;
                int          processSize = 0;
                while (processSize < size)
                {
                    if (size - processSize < 1024)
                    {
                        tmpdata = new byte[size - processSize];
                    }
                    else
                    {
                        tmpdata = new byte[1024];
                    }
                    fileStream.Write(info.data, processSize, tmpdata.Length);

                    processSize      += tmpdata.Length;
                    totalprocessSize += tmpdata.Length;
                    if (progress != null)
                    {
                        progress.SetProgressPercent(totalSize, totalprocessSize);
                    }
                }
            }
            fileStream.Flush();
            fileStream.Close();
            Debug.Log("打包完成");
        }
コード例 #11
0
        /** 同步解包一个文件夹 **/
        private static void UnPackFolder(object obj)
        {
            FileChangeInfo pathinfo = (FileChangeInfo)obj;
            string         inpath   = pathinfo.inpath;
            string         outpath  = pathinfo.outpath;
            CodeProgress   progress = null;

            if (pathinfo.progressDelegate != null)
            {
                progress = new CodeProgress(pathinfo.progressDelegate);
            }

            Dictionary <int, OneFileInfor> allFileInfoDic = new Dictionary <int, OneFileInfor>();

            System.Text.UTF8Encoding utf8Encoding = new System.Text.UTF8Encoding();


            int totalsize = 0;

            FileStream upkFilestream = new FileStream(inpath, FileMode.Open);

            upkFilestream.Seek(0, SeekOrigin.Begin);

            int offset = 0;

            //读取文件数量;
            byte[] totaliddata = new byte[4];
            upkFilestream.Read(totaliddata, 0, 4);
            int filecount = BitConverter.ToInt32(totaliddata, 0);

            offset += 4;
            Debug.Log("filecount=" + filecount);

            //读取所有文件信息;
            for (int index = 0; index < filecount; index++)
            {
                //读取id;
                byte[] iddata = new byte[4];
                upkFilestream.Seek(offset, SeekOrigin.Begin);
                upkFilestream.Read(iddata, 0, 4);
                int id = BitConverter.ToInt32(iddata, 0);
                offset += 4;

                //读取StartPos;
                byte[] startposdata = new byte[4];
                upkFilestream.Seek(offset, SeekOrigin.Begin);
                upkFilestream.Read(startposdata, 0, 4);
                int startpos = BitConverter.ToInt32(startposdata, 0);
                offset += 4;

                //读取size;
                byte[] sizedata = new byte[4];
                upkFilestream.Seek(offset, SeekOrigin.Begin);
                upkFilestream.Read(sizedata, 0, 4);
                int size = BitConverter.ToInt32(sizedata, 0);
                offset += 4;

                //读取pathLength;
                byte[] pathLengthdata = new byte[4];
                upkFilestream.Seek(offset, SeekOrigin.Begin);
                upkFilestream.Read(pathLengthdata, 0, 4);
                int pathLength = BitConverter.ToInt32(pathLengthdata, 0);
                offset += 4;

                //读取path;
                byte[] pathdata = new byte[pathLength];
                upkFilestream.Seek(offset, SeekOrigin.Begin);
                upkFilestream.Read(pathdata, 0, pathLength);
                string path = utf8Encoding.GetString(pathdata);
                offset += pathLength;


                //添加到Dic;
                OneFileInfor info = new OneFileInfor();
                info.id         = id;
                info.size       = size;
                info.pathLength = pathLength;
                info.path       = path;
                info.startPos   = startpos;
                allFileInfoDic.Add(id, info);

                totalsize += size;

                Debug.Log("id=" + id + " startPos=" + startpos + " size=" + size + " pathLength=" + pathLength + " path=" + path);
            }



            //释放文件;
            int totalprocesssize = 0;

            foreach (var infopair in allFileInfoDic)
            {
                OneFileInfor info = infopair.Value;

                int    startPos = info.startPos;
                int    size     = info.size;
                string path     = info.path;

                //创建文件;
                string dirpath  = outpath + path.Substring(0, path.LastIndexOf('/'));
                string filepath = outpath + path;
                //判断文件夹是否存在
                if (Directory.Exists(dirpath) == false)
                {
                    Directory.CreateDirectory(dirpath);
                }
                //判断如果已经有文件了就删除,再生成.(相当于替换)
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }

                FileStream fileStream = new FileStream(filepath, FileMode.Create);

                byte[] tmpfiledata;
                int    processSize = 0;
                while (processSize < size)
                {
                    if (size - processSize < 1024)
                    {
                        tmpfiledata = new byte[size - processSize];
                    }
                    else
                    {
                        tmpfiledata = new byte[1024];
                    }

                    //读取;
                    upkFilestream.Seek(startPos + processSize, SeekOrigin.Begin);
                    upkFilestream.Read(tmpfiledata, 0, tmpfiledata.Length);

                    //写入;
                    fileStream.Write(tmpfiledata, 0, tmpfiledata.Length);

                    processSize      += tmpfiledata.Length;
                    totalprocesssize += tmpfiledata.Length;
                    if (progress != null)
                    {
                        progress.SetProgressPercent((long)totalsize, (long)totalprocesssize);
                    }
                }
                fileStream.Flush();
                fileStream.Close();
            }
            upkFilestream.Close();
            Debug.Log("解包完成");
        }
コード例 #12
0
ファイル: LZMPMenu.cs プロジェクト: GameForMe/CardGame
    /// <summary>
    /// 从缓冲的目录中压缩;
    /// </summary>
    /// <param name="isAssetDir"> 是否是 asset 中的目录</param>
    private static void CompressLzmaFromDir(bool isAssetDir)
    {
        if (autoPathArr == null || autoPathArr.Count <= 0)
        {
            return;
        }
        string assetBundlePath = Application.dataPath + "/StreamingAssets/Data";
        string lzmaFilePath    = assetBundlePath + "/Data." + LzmaTools.zipName;

        if (!Directory.Exists(assetBundlePath))
        {
            Directory.CreateDirectory(assetBundlePath);
        }
        try
        {
            if (File.Exists(lzmaFilePath))
            {
                File.Delete(lzmaFilePath);
            }

            MemoryStream memoryStream   = new MemoryStream();
            FileStream   compressStream = new FileStream(lzmaFilePath, FileMode.OpenOrCreate, FileAccess.Write);

            int    lastIndex     = Application.dataPath.LastIndexOf("/");
            string prePath       = Application.dataPath.Substring(0, lastIndex + 1);
            int    filePathCount = autoPathArr.Count;
            for (int i = 0; i < filePathCount; i++)
            {
                string assetPath = autoPathArr[i];
                if (assetPath != null && assetPath != "")
                {
                    string filePath      = assetPath;
                    string zipBundlePath = "";
                    if (isAssetDir)
                    {
                        zipBundlePath = assetPath.Replace("Assets/", "");
                    }
                    else
                    {
                        zipBundlePath = assetPath.Replace(Directory.GetCurrentDirectory() + "/../", "");
                        filePath      = assetPath;
                        zipBundlePath = zipBundlePath.Replace("DataTXT", "Data");
                    }


                    FileStream tempFileStream = File.Open(filePath, FileMode.Open);

                    StringBuilder sb = new StringBuilder();                     // set header info: path + filesie + separator
                    sb.Append(zipBundlePath).Append(",").Append(tempFileStream.Length).Append("\n");

                    byte[] tempBuff = new byte[tempFileStream.Length];
                    byte[] header   = Encoding.UTF8.GetBytes(sb.ToString());
                    tempFileStream.Read(tempBuff, 0, (int)tempFileStream.Length);                         // get file data

                    memoryStream.Write(header, 0, header.Length);
                    memoryStream.Write(tempBuff, 0, tempBuff.Length);

                    tempFileStream.Close();
                }
            }

            // important !!!
            memoryStream.Position = 0;

            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();

            encoder.WriteCoderProperties(compressStream);

            byte[] compressLen = new byte[8];  // file size
            for (int i = 0; i < compressLen.Length; i++)
            {
                compressLen[i] = (byte)(memoryStream.Length >> (8 * i));
            }
            compressStream.Write(compressLen, 0, 8);

            CodeProgress codeProgress = new CodeProgress(); // compress
            codeProgress.totalSize = memoryStream.Length;
            encoder.Code(memoryStream, compressStream, memoryStream.Length, -1, codeProgress);

            memoryStream.Flush();
            memoryStream.Close();
            compressStream.Close();

            AssetDatabase.Refresh(); // refresh asssets
            EditorUtility.ClearProgressBar();

            Debug.Log("success  compress ");
        }
        catch (Exception exe)
        {
            Debug.LogError(exe.Message);
        }
    }
コード例 #13
0
ファイル: MyPackRes.cs プロジェクト: syjsu/LZMA-with-Unity3D
    /**  打包一个文件夹  **/
    public static void PackFolder(string folderpath, string upkfilepath, CodeProgress progress)
    {
        TraverseFolder(folderpath);

        Debug.Log("文件数量 : " + m_id + "个");
        Debug.Log("文件总大小 : " + m_totalSize + "KB");

        /**  更新文件在UPK中的起始点  **/
        int firstfilestartpos = 0 + 4;

        for (int index = 0; index < m_allFileInfoDic.Count; index++)
        {
            firstfilestartpos += 4 + 4 + 4 + 4 + m_allFileInfoDic[index].m_PathLength;
        }

        int startpos = 0;

        for (int index = 0; index < m_allFileInfoDic.Count; index++)
        {
            if (index == 0)
            {
                startpos = firstfilestartpos;
            }
            else
            {
                startpos = m_allFileInfoDic[index - 1].m_StartPos + m_allFileInfoDic[index - 1].m_Size;                //上一个文件的开始+文件大小;
            }

            m_allFileInfoDic[index].m_StartPos = startpos;
        }

        /**  写文件  **/
        FileStream fileStream = new FileStream(upkfilepath, FileMode.Create);

        /**  文件总数量  **/
        byte[] totaliddata = System.BitConverter.GetBytes(m_id);
        fileStream.Write(totaliddata, 0, totaliddata.Length);

        for (int index = 0; index < m_allFileInfoDic.Count; index++)
        {
            /** 写入ID **/
            byte[] iddata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_id);
            fileStream.Write(iddata, 0, iddata.Length);

            /**  写入StartPos  **/
            byte[] startposdata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_StartPos);
            fileStream.Write(startposdata, 0, startposdata.Length);

            /**  写入size  **/
            byte[] sizedata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_Size);
            fileStream.Write(sizedata, 0, sizedata.Length);

            /**  写入pathLength  **/
            byte[] pathLengthdata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_PathLength);
            fileStream.Write(pathLengthdata, 0, pathLengthdata.Length);

            /**  写入path  **/
            byte[] mypathdata = new UTF8Encoding().GetBytes(m_allFileInfoDic[index].m_Path);

            fileStream.Write(mypathdata, 0, mypathdata.Length);
        }

        /**  写入文件数据  **/
//		for (int index = 0; index < m_allFileInfoDic.Count; index++)
//		{
//			fileStream.Write(m_allFileInfoDic[index].m_data, 0, m_allFileInfoDic[index].m_Size);
//		}
        int totalprocessSize = 0;

        foreach (var infopair in m_allFileInfoDic)
        {
            OneFileInfor info        = infopair.Value;
            int          size        = info.m_Size;
            byte[]       tmpdata     = null;
            int          processSize = 0;
            while (processSize < size)
            {
                if (size - processSize < 1024)
                {
                    tmpdata = new byte[size - processSize];
                }
                else
                {
                    tmpdata = new byte[1024];
                }
                fileStream.Write(info.m_data, processSize, tmpdata.Length);

                processSize      += tmpdata.Length;
                totalprocessSize += tmpdata.Length;

                progress.SetProgressPercent(m_totalSize, totalprocessSize);
            }
        }

        fileStream.Flush();
        fileStream.Close();


        /** 重置数据 **/
        m_id        = 0;
        m_totalSize = 0;
        m_allFileInfoDic.Clear();
    }
コード例 #14
0
ファイル: ABTest.cs プロジェクト: feixus/UnityLzmaTest
	/// <summary>
	/// Compresses the lzma.
	/// </summary>
    private static void CompressLzma()
    {
        if(resourcesLis == null || resourcesLis.Count <= 0)
        {
            return;
        }

        string assetBundlePath = Application.dataPath + "/AssetBundle";
        string lzmaFilePath = assetBundlePath + "/AssetBundle.lzma";
        if(!Directory.Exists(assetBundlePath))
        {
            Directory.CreateDirectory(assetBundlePath);
        }

        try
        {
            MemoryStream memoryStream = new MemoryStream();
            FileStream compressStream = new FileStream(lzmaFilePath, FileMode.OpenOrCreate, FileAccess.Write);

            int lastIndex = Application.dataPath.LastIndexOf("/");
            string prePath = Application.dataPath.Substring(0, lastIndex + 1);
            int filePathCount = resourcesLis.Count;
            for (int i = 0; i < filePathCount; i++)
            {
                string assetPath = AssetDatabase.GetAssetPath(resourcesLis[i]);
                string filePath = prePath + assetPath;
                string zipBundlePath = assetPath.Replace("Assets/", "");

                FileStream tempFileStream = File.Open(filePath, FileMode.Open);

                StringBuilder sb = new StringBuilder(); // set header info: path + filesie + separator
                sb.Append(zipBundlePath).Append(",").Append(tempFileStream.Length).Append("\n");

                byte[] tempBuff = new byte[tempFileStream.Length];
                byte[] header = Encoding.UTF8.GetBytes(sb.ToString());
                tempFileStream.Read(tempBuff, 0, (int)tempFileStream.Length);     // get file data

                memoryStream.Write(header, 0, header.Length);
                memoryStream.Write(tempBuff, 0, tempBuff.Length);

                tempFileStream.Close();
            }

            // important !!!
            memoryStream.Position = 0;

            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();

            encoder.WriteCoderProperties(compressStream);

            byte[] compressLen = new byte[8];  // file size
            for (int i = 0; i < compressLen.Length; i++)
            {
                compressLen[i] = (byte)(memoryStream.Length >> (8 * i));
            }
            compressStream.Write(compressLen, 0, 8);

            CodeProgress codeProgress = new CodeProgress(); // compress
            codeProgress.totalSize = memoryStream.Length;
            encoder.Code(memoryStream, compressStream, memoryStream.Length, -1, codeProgress);

            memoryStream.Flush();
            memoryStream.Close();
            compressStream.Close();

            AssetDatabase.Refresh(); // refresh asssets
            EditorUtility.ClearProgressBar();
        }
        catch (Exception exe)
        {
            Debug.Log(exe.Message);
        }
    }