Monotouch - ICSharpCode.SharpZipLib 出现错误

发布于 2024-10-10 09:03:43 字数 3712 浏览 5 评论 0原文

大家好,

我正在尝试使用 ICSharpCode.SharpZipLib 库生成 Zip 文件,但它抛出了一个非常奇怪的错误。

代码:

public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)       
{
        ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
        int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

        TrimLength += 1; //remove '\'
        FileStream ostream;
        byte[] obuffer;

        ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outputPathAndFile)); // create zip stream
        if (password != null && password != String.Empty)
            oZipStream.Password = password;
        oZipStream.SetLevel(9); // maximum compression
        ZipEntry oZipEntry;
        foreach (string Fil in ar) // for each file, generate a zipentry
        {
            oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
            oZipStream.PutNextEntry(oZipEntry);

            if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
            {
                ostream = File.OpenRead(Fil);
                obuffer = new byte[ostream.Length];
                ostream.Read(obuffer, 0, obuffer.Length);
                oZipStream.Write(obuffer, 0, obuffer.Length);
            }
        }
        oZipStream.Finish();
        oZipStream.Close();
}


private static ArrayList GenerateFileList(string Dir)
{
        ArrayList fils = new ArrayList();
        bool Empty = true;
        foreach (string file in Directory.GetFiles(Dir,"*.xml")) // add each file in directory
        {
            fils.Add(file);
            Empty = false;
        }

        if (Empty)
        {
            if (Directory.GetDirectories(Dir).Length == 0)
                // if directory is completely empty, add it
            {
                fils.Add(Dir + @"/");
            }
        }

        foreach (string dirs in Directory.GetDirectories(Dir)) // recursive
        {
            foreach (object obj in GenerateFileList(dirs))
            {
                fils.Add(obj);
            }
        }
        return fils; // return file list
}

错误:

Unhandled Exception: System.NotSupportedException: CodePage 437 not supported
  at System.Text.Encoding.GetEncoding (Int32 codepage) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipConstants.ConvertToArray (System.String str) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipConstants.ConvertToArray (Int32 flags, System.String str) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipOutputStream.PutNextEntry (ICSharpCode.SharpZipLib.Zip.ZipEntry entry) [0x00000] in <filename unknown>:0 
  at WpfPrototype1.MainInvoicesView.ZipFiles (System.String inputFolderPath, System.String outputPathAndFile, System.String password) [0x00000] in <filename unknown>:0 
  at WpfPrototype1.MainInvoicesView.<ViewDidLoad>m__6 (System.Object , System.EventArgs ) [0x00000] in <filename unknown>:0 
  at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in <filename unknown>:0 
  at WpfPrototype1.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 

如何使此代码支持 CodePage 437?

问候,
克劳迪奥

Hy guys,

I'm trying to generate a Zip File with ICSharpCode.SharpZipLib library but it's throwing a really weird error.

Code:

public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)       
{
        ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
        int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

        TrimLength += 1; //remove '\'
        FileStream ostream;
        byte[] obuffer;

        ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outputPathAndFile)); // create zip stream
        if (password != null && password != String.Empty)
            oZipStream.Password = password;
        oZipStream.SetLevel(9); // maximum compression
        ZipEntry oZipEntry;
        foreach (string Fil in ar) // for each file, generate a zipentry
        {
            oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
            oZipStream.PutNextEntry(oZipEntry);

            if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
            {
                ostream = File.OpenRead(Fil);
                obuffer = new byte[ostream.Length];
                ostream.Read(obuffer, 0, obuffer.Length);
                oZipStream.Write(obuffer, 0, obuffer.Length);
            }
        }
        oZipStream.Finish();
        oZipStream.Close();
}


private static ArrayList GenerateFileList(string Dir)
{
        ArrayList fils = new ArrayList();
        bool Empty = true;
        foreach (string file in Directory.GetFiles(Dir,"*.xml")) // add each file in directory
        {
            fils.Add(file);
            Empty = false;
        }

        if (Empty)
        {
            if (Directory.GetDirectories(Dir).Length == 0)
                // if directory is completely empty, add it
            {
                fils.Add(Dir + @"/");
            }
        }

        foreach (string dirs in Directory.GetDirectories(Dir)) // recursive
        {
            foreach (object obj in GenerateFileList(dirs))
            {
                fils.Add(obj);
            }
        }
        return fils; // return file list
}

Error:

Unhandled Exception: System.NotSupportedException: CodePage 437 not supported
  at System.Text.Encoding.GetEncoding (Int32 codepage) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipConstants.ConvertToArray (System.String str) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipConstants.ConvertToArray (Int32 flags, System.String str) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipOutputStream.PutNextEntry (ICSharpCode.SharpZipLib.Zip.ZipEntry entry) [0x00000] in <filename unknown>:0 
  at WpfPrototype1.MainInvoicesView.ZipFiles (System.String inputFolderPath, System.String outputPathAndFile, System.String password) [0x00000] in <filename unknown>:0 
  at WpfPrototype1.MainInvoicesView.<ViewDidLoad>m__6 (System.Object , System.EventArgs ) [0x00000] in <filename unknown>:0 
  at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in <filename unknown>:0 
  at WpfPrototype1.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 

How can I make this code support CodePage 437?

Regards,
Claudio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋意浓 2024-10-17 09:03:43

MonoTouch 删除了它无法静态确定您需要的 I18N 代码页。在这种情况下,您可以通过以下两种方式之一强制 monotouch 保留所需的代码页集合(West):

  1. 单击 Project->[ProjectName] Options
  2. 选择 iPhone Build
  3. 此时您有两个选项
    一个。从 I18n 程序集列表中选择“west”
    b.将“-i18n=west”添加到“额外参数”中

注意:您需要对每个配置和平台组合执行步骤 #3。

MonoTouch removes I18N codepages that it cannot statically determine that you need. You can force monotouch to keep the needed codepage collection (West) in this case one of two ways:

  1. Click Project->[ProjectName] Options
  2. Select iPhone Build
  3. You have two options at this point
    a. Select "west" from the I18n assemblies list
    b. Add "-i18n=west" to "Extra Arguments"

NOTE: You will need to perform step #3 for every combination of configurations and platforms.

尬尬 2024-10-17 09:03:43

我知道这是一个旧线程,但我刚刚花了一个下午尝试用 monodroid 4.6 修复这个问题。在以前的版本中,技巧是手动添加对 I18N 和 I18N.WEST 库的引用,但现在从版本 4.6 开始,它不再起作用了。 :(

所以我对 SharpZipLib 的 ZipConstants.cs 文件进行了修复:

从:

static int defaultCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;

到:

static int defaultCodePage = System.Text.Encoding.UTF8.CodePage;

I know this is an old thread, but I just spent an afternoon trying to fix this with monodroid 4.6. On the previous version the trick was to manually add a reference to the I18N and I18N.WEST libraries, but now starting with version 4.6 it doesn't work anymore. :(

So I applied a fix to the ZipConstants.cs file of SharpZipLib:

From:

static int defaultCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;

To:

static int defaultCodePage = System.Text.Encoding.UTF8.CodePage;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文