LoginSignup
6
6

More than 5 years have passed since last update.

Unity C#(Mono)でgZip圧縮を行うには

Last updated at Posted at 2015-06-05

下記ページ参照
 http://melan260.net/?p=530

下記URLに解決策

 http://forum.unity3d.com/threads/cant-use-gzipstream-from-c-behaviours.33973/

 ICSharpCode.SharpZipLib.dllが必要

using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.BZip2;

[System.Serializable]
public struct Zipper
{
    public static string ZipString(string sBuffer)
    {
        MemoryStream m_msBZip2 = null;
        BZip2OutputStream m_osBZip2 = null;
        string result;
        try
        {
            m_msBZip2 = new MemoryStream();
            Int32 size = sBuffer.Length;
            // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)
            //
            using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII))
            {
                writer.Write( size );

                m_osBZip2 = new BZip2OutputStream(m_msBZip2);
                m_osBZip2.Write(Encoding.ASCII.GetBytes(sBuffer), 0, sBuffer.Length);

                m_osBZip2.Close();
                result = Convert.ToBase64String(m_msBZip2.ToArray());
                m_msBZip2.Close();

                writer.Close();
            }
        }
        finally
        {
            if (m_osBZip2 != null)
            {
                m_osBZip2.Dispose();
            }
            if (m_msBZip2 != null)
            {
                m_msBZip2.Dispose();
            }
        }
        return result;
    }

    public static string UnzipString(string compbytes)
    {
        string result;
        MemoryStream m_msBZip2 = null;
        BZip2InputStream m_isBZip2 = null;
        try
        {
            m_msBZip2 = new MemoryStream(Convert.FromBase64String(compbytes));
            // read final uncompressed string size stored in first 4 bytes
            //
            using (BinaryReader reader = new BinaryReader(m_msBZip2, System.Text.Encoding.ASCII))
            {
                Int32 size = reader.ReadInt32();

                m_isBZip2 = new BZip2InputStream(m_msBZip2);
                byte[] bytesUncompressed = new byte[size];
                m_isBZip2.Read(bytesUncompressed, 0, bytesUncompressed.Length);
                m_isBZip2.Close();
                m_msBZip2.Close();

                result = Encoding.ASCII.GetString(bytesUncompressed);

                reader.Close();
            }
        }
        finally
        {
            if (m_isBZip2 != null)
            {
                m_isBZip2.Dispose();
            }
            if (m_msBZip2 != null)
            {
                m_msBZip2.Dispose();
            }
        }
        return result;
    }
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6