Unity截屏&&压缩解压Zip

来源:互联网 发布:2017网络彩票代理加盟 编辑:程序博客网 时间:2024/04/29 07:18

  StartCoroutine(GetCapture());   

 IEnumerator GetCapture()

        {
            yield return new WaitForEndOfFrame();
            int width = Screen.width;
            int height = Screen.height;
            Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
            tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, true);

            byte[] imagebytes = tex.EncodeToPNG();

        //  byte[] imagebytes = tex.EncodeToJPG(0~100);

          //  tex.Compress(false);

          //  tex.Apply();

            StringBuilder strPath = new StringBuilder();
            System.DateTime t = System.DateTime.Now;
            // strPath.Append(Application.persistentDataPath + "/" + "Pngs" + "/");
            strPath.Append(Application.persistentDataPath + "/" + "Pngs" + "/");
            System.IO.Directory.CreateDirectory(strPath.ToString());
            strPath.AppendFormat("{0}_{1:D2}_{2:D2}_{3:D2}_{4:D2}_{5:D2}_{6}_Shot.png", t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second, t.Millisecond);
            File.WriteAllBytes(strPath.ToString(), imagebytes);
            //-- File.WriteAllBytes(Application.dataPath + "/screencapture.png", imagebytes);
            Debug.Log("GetCapture:" + strPath.ToString());


        }

//-----------------------------------------------------------------------------------------------------------------------------------

压缩zip

ICSharpCode.SharpZipLib.dll

http://icsharpcode.github.io/SharpZipLib/

using UnityEngine;
using System.Collections;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;




public class ZipHelper
{
    public static void GoCompress(string SourceFile, string TartgetFile)
    {
        string Source = SourceFile;
        string Tartget = TartgetFile;
        Directory.CreateDirectory(Path.GetDirectoryName(Tartget));
        using (ZipOutputStream s = new ZipOutputStream(File.Create(Tartget)))
        {
            s.SetLevel(6);
            Compress(Source, s);
            s.Finish();
            s.Close();
        }


    }
    /// <summary>
    /// 压缩文件夹
    /// </summary>
    /// <param name="source">源目录</param>
    /// <param name="s">ZipOutputStream对象</param>
    public static void Compress(string source, ZipOutputStream s)
    {
        string[] filenames = Directory.GetFileSystemEntries(source);
        foreach (string file in filenames)
        {
            if (Directory.Exists(file))
            {
                // 递归压缩子文件夹
                Compress(file, s);
            }
            else
            {
                using (FileStream fs = File.OpenRead(file))
                {
                    byte[] buffer = new byte[4 * 1024];
                    // 此处去掉盘符,如D:\123\1.txt 去掉D:
                    ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file), ""));
                  //  Debug.Log("===============:"+file.Replace(Path.GetPathRoot(file), ""));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    int sourceBytes;
                    do
                    {
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
                        s.Write(buffer, 0, sourceBytes);
                    } while (sourceBytes > 0);
                }
            }
        }
    }


    /// <summary>
    /// 解压缩
    /// </summary>
    /// <param name="sourceFile">压缩包完整路径地址</param>
    /// <param name="targetPath">解压路径是哪里</param>
    /// <returns></returns>
    public static bool Decompress(string sourceFile, string targetPath)
    {
        if (!File.Exists(sourceFile))
        {
            throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));
        }
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }
        using (var s = new ZipInputStream(File.OpenRead(sourceFile)))
        {
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                if (theEntry.IsDirectory)
                {
                    continue;
                }
                string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));
                string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
                if (!Directory.Exists(directorName))
                {
                    Directory.CreateDirectory(directorName);
                }
                if (!String.IsNullOrEmpty(fileName))
                {
                    using (FileStream streamWriter = File.Create(fileName))
                    {
                        int size = 4096;
                        byte[] data = new byte[size];
                        while (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                            size = s.Read(data, 0, data.Length);
                        }
                    }
                }
            }
        }
        return true;
    }
}



0 0
原创粉丝点击