Monotouch - ICSharpCode.SharpZipLib дает ошибку - PullRequest
13 голосов
/ 05 января 2011

Hy, ребята,

Я пытаюсь сгенерировать Zip-файл с библиотекой ICSharpCode.SharpZipLib, но он выдает действительно странную ошибку.

Код:

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?

С уважением,
Claudio

Ответы [ 2 ]

17 голосов
/ 05 января 2011

MonoTouch удаляет кодовые страницы I18N, которые он не может статически определить, что вам нужно. Вы можете заставить monotouch сохранить необходимую коллекцию кодовых страниц (запад) в этом случае одним из двух способов:

  1. Нажмите Project -> [ProjectName] Опции
  2. Выберите iPhone Build
  3. У вас есть два варианта на данный момент а. Выберите «запад» из списка сборок I18n б. Добавьте "-i18n = west" к "Extra Arguments"

ПРИМЕЧАНИЕ. Вам потребуется выполнить шаг № 3 для каждой комбинации конфигураций и платформ.

4 голосов
/ 01 апреля 2013

Я знаю, что это старая ветка, но я провел целый день, пытаясь исправить это с помощью monodroid 4.6.В предыдущей версии трюк заключался в том, чтобы вручную добавить ссылку на библиотеки I18N и I18N.WEST, но теперь, начиная с версии 4.6, она больше не работает.: (

Поэтому я применил исправление к файлу ZipConstants.cs SharpZipLib:

От:

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

Кому:

static int defaultCodePage = System.Text.Encoding.UTF8.CodePage;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...