13

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

Claudio
  • 1,987
  • 3
  • 29
  • 55
  • According to [this bug report][1] this was fixed years ago. What version of mono are you working with? [1]: https://bugs.launchpad.net/ubuntu/+source/mono/+bug/235858 – SilverbackNet Jan 05 '11 at 05:09
  • I updated it last week. I don't think it's an update problem. – Claudio Jan 05 '11 at 22:52

2 Answers2

17

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.

Geoff Norton
  • 5,056
  • 1
  • 19
  • 17
  • Yeah, it works. But just a question: if I'm working with zip files, it's gonna use something like Huffman's coding, reading the file/data bit per bit. So it wasn't supposed to ask about codepage, right? – Claudio Jan 05 '11 at 10:29
  • I have no idea how SharpZipLib is implemented, so I couldn't comment why they need/want this codepage. – Geoff Norton Jan 05 '11 at 18:11
  • 3
    Thanks for this tip! I had a similar problem, but with a different codepage. Here is the list of codepages and their corresponding internationalization options: http://docs.xamarin.com/ios/advanced_topics/internationalization – Jacob Foshee May 01 '12 at 22:52
  • This also affects MonoDroid. In addition to the fix above, another workaround is required for Android, similar to http://stackoverflow.com/questions/10858690/get-encoding-fails-when-i-build-monodroid-project-with-unshared-runtime – Andy Joiner Jan 17 '13 at 13:57
4

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;
Fabrizio Farenga
  • 460
  • 1
  • 5
  • 16
  • I had the same problem, this did the trick, however it would be nice with a better, more "clean" fix than this... – Miros May 16 '13 at 01:26
  • Be careful with this solution! In my case it worked too but I was not able to unzip those files afterwards. It came up with strange errors like "Central directory not found.". – Alexander Schmidt May 28 '13 at 18:02
  • I wonder why authors of SharpZibLib use OEMCodePage there. Did they mean ANSICodePage? – Der_Meister Mar 13 '17 at 08:25