嗨,伙计们,
我试图用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?
致以敬意,
克劳迪奥
发布于 2011-01-05 13:05:03
MonoTouch删除了无法静态确定您所需的I18N代码页。在这种情况下,您可以通过以下两种方式之一强制monotouch保留所需的代码页集合(West):
注意:对于每种配置和平台组合,您都需要执行步骤#3。
发布于 2013-04-01 05:07:43
我知道这是一个老问题,但是我花了一个下午的时间尝试用mondroid4.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;https://stackoverflow.com/questions/4600923
复制相似问题