单声道无法压缩?

时间:2014-10-30 20:22:02

标签: c# unity3d mono deflatestream

我正试图压缩像单声道这样的数据:

public static string Save(FlightProgram program, bool compressed)
{
    using (MemoryStream ms = new MemoryStream())
    {
        BinaryFormatter f = new BinaryFormatter();
        if (compressed)
        {
            using (DeflateStream gz = new DeflateStream(ms, CompressionMode.Compress))
            {
                f.Serialize(gz, program);
            }
        }
        else
        {
            f.Serialize(ms, program);
        }
        return Convert.ToBase64String(ms.ToArray()).Replace('/', '_');
    }
}

我只是得到了异常“CreateZStream”。没有内在的例外。这是怎么回事?

Stacktrace:

Could not save flight program: CreateZStream at   at (wrapper managed-to-native) System.IO.Compression.DeflateStream:CreateZStream (System.IO.Compression.CompressionMode,bool,System.IO.Compression.DeflateStream/UnmanagedReadOrWrite,intptr)
    at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen, Boolean gzip) [0x00000] in <filename unknown>:0 
    at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode) [0x00000] in <filename unknown>:0 
    at KSPComputerModule.ProgramSerializer.Save (KSPComputer.FlightProgram program, Boolean compressed) [0x00000] in <filename unknown>:0 
    at KSPComputerModule.FPComputer.OnSave (.ConfigNode node) [0x00000] in <filename unknown>:0  (State: None)

2 个答案:

答案 0 :(得分:6)

当然与OP无关......但我终于设法解决了这个问题。这是Mono for Windows部署中的一个错误。如果从Mono / GtkSharp目录中删除MonoPosixHelper.dll个文件,您将突然发现它可以正常工作。运行Process Monitor,您可以看到,而不是针对编译错误的MonoPosixHelper.dll,它现在适用于libMonoPosixHelper.dll ...这是正确编译的。

因此,要永久解决,请删除所有MonoPosixHelper.dll文件,使其使用libMonoPosixHelper.dll ...或者,最好使用dllmap,添加

 <dllmap dll="MonoPosixHelper" target="libMonoPosixHelper.dll" os="windows" />

C:\Program Files (x86)\Mono\lib\mono\gac\System\4.0.0.0__b77a5c561934e089\System.dll.config(如果它不存在则创建)或C:\Program Files (x86)\Mono\etc\mono\config

的全局Mono配置

答案 1 :(得分:5)

我自己测试过,得到了一个DllNotFoundException:

System.DllNotFoundException: MonoPosixHelper
  at (wrapper managed-to-native) System.IO.Compression.DeflateStream:CreateZStream (System.IO.Compression.CompressionMode,bool,System.IO.Compression.DeflateStream/UnmanagedReadOrWrite,intptr)
  at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen, Boolean gzip) [0x00000] in <filename unknown>:0 
  at System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.Compression.DeflateStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode)

原因似乎是CreateZStream函数,它是一个外部方法,应该在MonoPosixHelper.dll中定义。

CreateZStream声明为

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr CreateZStream(CompressionMode compress, bool gzip, DeflateStream.UnmanagedReadOrWrite feeder, IntPtr data);

但是文件“MonoPosixHelper.dll”不存在。有一个libMonoPosixHelper.dylib似乎实现了CreateZStream的入口点,但是这是一个用于mac的库。

我个人对Unity中的大多数压缩代码/库都有不好的经历,因为大多数实现只是包装一个本机代码库。根据{{​​3}}支持该类,但它看起来只有compatibility pagemanaged part is implemented似乎也验证了这一点。

我在很多项目中成功使用了This one库。它是一个没有本机代码依赖性的纯托管库。我已经在独立,web,android和iOS版本中使用了这个库而没有任何问题。