非英语语言环境中的文件损坏(编码问题?) [英] Currupted file in non-english locale (encoding problem?)

查看:161
本文介绍了非英语语言环境中的文件损坏(编码问题?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSI Windows Installer中,我有一个自定义的VBScript操作,该操作将某些文件从二进制"表中提取到文件系统中.这是我正在使用的代码:

In my MSI Windows Installer I have a custom VBScript action which extracts some files from the 'Binary' table to the filesystem. This is the code I'm using:

灵感来自 : https://www.itninja.com/question/how-to-通过msi中的vbscript自定义动作调用存储在二进制表中的一个exe

Function ExtractFromBinary(ByVal binaryName, ByVal binaryOutputFile)

 Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

 Const msiReadStreamInteger = 0
 Const msiReadStreamBytes = 1
 Const msiReadStreamAnsi = 2 
 Const msiReadStreamDirect = 3

 Dim binaryView : Set binaryView = Session.Database.OpenView("SELECT Data FROM Binary WHERE Name = '" & binaryName & "'") 
 binaryView.Execute

 Dim binaryRecord : Set binaryRecord = binaryView.Fetch 
 Dim binaryData : binaryData = binaryRecord.ReadStream(1, binaryRecord.DataSize(1), msiReadStreamAnsi) 
 Set binaryRecord = Nothing

 Dim binaryStream : Set binaryStream = oFSO.CreateTextFile(binaryOutputFile, True, False) 
 binaryStream.Write binaryData
 binaryStream.Close
 Set binaryStream = Nothing 

End Function

至今已使用2-3年,没有任何问题.但是,现在在日语Windows安装中出现一种情况,其中提取的二进制文件已损坏:

This has been used without any issues in production for 2-3 years now. However now we have a case on a Japanese Windows installation where the extracted binary files are corrupted:

如您所见,问题通常在?"之后脚本在其中插入"E"或覆盖以下字符.

As you can see, the problem typically after a '?' where the script either inserts an 'E', or overwrites the following character.

ReadStream方法和CreateTextFile方法都具有影响编码的参数.上面显示的组合似乎是唯一可以在我的英语Windows 10上运行的组合.

Both the ReadStream method and the CreateTextFile method have a parameter which affect encoding. The combination shown above seems to be the only one which works on my English Windows 10.

我需要在上面的代码中进行哪些更改才能使其在日语系统上正常工作?

What do I need to change in the code above to make it work also on a Japanese system?

推荐答案

这就是我最终得到的结果.

Here is what I ended up with.

按照 SteinÅsmul的建议,我使用C#(.NET/DTF)重新编写了自定义操作.最初,我很犹豫用C#编写自定义操作,因为它为安装程序引入了其他先决条件.但是事实证明,如果自定义操作的目标是.NET Framework 2.0,则大多数机器都应支持该操作,而无需手动安装该框架(请参阅

As suggested by Stein Åsmul I rewrote the custom action using C# (.NET / DTF). Initially I was hesitant to writing custom actions in C# as it introduces additional prerequisites to the installer. But it turns out that if the custom action targets .NET Framework 2.0, it should be supported on most machines without the need to manually install the framework (see here).

这是我的代码:

public static class TemporaryFilesExtractor
{

    [CustomAction]
    public static ActionResult ExtractTemporaryFiles(Session session)
    {
        ExtractFromBinary(session, "binaryname1", "<filePath1>");
        ExtractFromBinary(session, "binaryname2", "<filePath2>");
        return ActionResult.Success;
    }

    private static void ExtractFromBinary(Session session, string binaryName, string binaryOutputFile)
    {
        session.Log($"Extracting {binaryName} to {binaryOutputFile}");
        byte[] buffer = new byte[4096];

        using (var view = session.Database.OpenView("SELECT Data FROM Binary WHERE Name = '{0}'", binaryName))
        {
            view.Execute();
            using (var record = view.Fetch())
            using (var dbStream = record.GetStream(1))
            using (var fileStream = File.OpenWrite(binaryOutputFile))
            {
                int count;
                while ((count = dbStream.Read(buffer, 0, buffer.Length)) != 0)
                    fileStream.Write(buffer, 0, count);
            }
        }
    }

}

这篇关于非英语语言环境中的文件损坏(编码问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆