Skip to content

Instantly share code, notes, and snippets.

@5argon
Last active June 12, 2017 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5argon/aa4eca55d68551537e0cd76598bf0642 to your computer and use it in GitHub Desktop.
Save 5argon/aa4eca55d68551537e0cd76598bf0642 to your computer and use it in GitHub Desktop.
Compress, decompress example/benchmark test.
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Encryption;
class SharpZibLibTest
{
private void SaveBinary(YourClass s, string savePath)
{
Stopwatch sw = new Stopwatch();
for (int i = 0; i <= 9; i++)
{
sw.Reset();
sw.Start();
FileStream file = File.Create(savePath + i);
ZipOutputStream zos = new ZipOutputStream(file);
zos.SetLevel(i);
zos.Password = "Hello";
zos.IsStreamOwner = true;
ZipEntry entry = new ZipEntry("myEntry");
zos.PutNextEntry(entry);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(zos, s);
zos.CloseEntry();
zos.Close();
file.Close();
sw.Stop();
UnityEngine.Debug.Log("Elapsed : " + sw.ElapsedMilliseconds);
}
}
private static YourClass LoadFromStream(Stream file)
{
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
Stopwatch sw = new Stopwatch();
BinaryFormatter bf = new BinaryFormatter();
sw.Start();
YourClass result = null;
using (ZipInputStream zis = new ZipInputStream(file))
{
zis.Password = "Hello";
ZipEntry entry = zis.GetNextEntry();
result = (YourClass)bf.Deserialize(zis);
zis.Close();
}
file.Close();
sw.Stop();
UnityEngine.Debug.Log("Elapsed : " + sw.ElapsedMilliseconds);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment