Monotouch - ICSharpCode.SharpZipLib 用于解压缩文件

发布于 2024-10-15 03:05:14 字数 4382 浏览 8 评论 0原文

我正在使用 ICSharpCode.SharpZipLib 解压一个包含文件、文件夹和子文件夹的文件,但我收到了一个错误,我在此处或任何其他论坛中都没有找到该错误。

代码:

public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
{
        ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
        if (password != null && password != String.Empty)
            s.Password = password;
        ZipEntry theEntry;
        string tmpEntry = String.Empty;
        while ((theEntry = s.GetNextEntry()) != null)
        {
            string directoryName = outputFolder;
            string fileName = Path.GetFileName(theEntry.Name);
            // create directory 
            if (directoryName != "")
            {
                Directory.CreateDirectory(directoryName);
            }
            if (fileName != String.Empty)
            {
                if (theEntry.Name.IndexOf(".ini") < 0)
                {
                    string fullPath = directoryName + "\\" + theEntry.Name;
                    fullPath = fullPath.Replace("\\ ", "\\");
                    string fullDirPath = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                    FileStream streamWriter = File.Create(fullPath);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
        }
        s.Close();
        if (deleteZipFile)
        {
            File.Delete(zipPathAndFile);

        }
}

错误得到:

Unhandled Exception: ICSharpCode.SharpZipLib.SharpZipBaseException: Unknown block type 6
  at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Decode () [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Inflate (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.BodyRead (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.InitialRead (System.Byte[] destination, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.CloseEntry () [0x00000] in <filename unknown>:0Error connecting stdout and stderr (127.0.0.1:10001)

  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry () [0x00000] in <filename unknown>:0 
  at FolderNavigation.ZipController.UnZipFiles (System.String zipPathAndFile, System.String outputFolder, System.String password, Boolean deleteZipFile) [0x00119] in /Users/claudio/Projects/FolderNavigation/FolderNavigation/ZipController.cs:15 
  at FolderNavigation.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x0005e] in /Users/claudio/Projects/FolderNavigation/FolderNavigation/Main.cs:43 
  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) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31 
  at FolderNavigation.Application.Main (System.String[] args) [0x00000] in /Users/claudio/Projects/FolderNavigation/FolderNavigation/Main.cs:14 

有什么想法吗?

问候,
克劳迪奥

I'm using ICSharpCode.SharpZipLib for unzip a file that contain file, folders and subfolders inside, but i'm getting an error that I didn't find here or in any other forum.

Code:

public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
{
        ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
        if (password != null && password != String.Empty)
            s.Password = password;
        ZipEntry theEntry;
        string tmpEntry = String.Empty;
        while ((theEntry = s.GetNextEntry()) != null)
        {
            string directoryName = outputFolder;
            string fileName = Path.GetFileName(theEntry.Name);
            // create directory 
            if (directoryName != "")
            {
                Directory.CreateDirectory(directoryName);
            }
            if (fileName != String.Empty)
            {
                if (theEntry.Name.IndexOf(".ini") < 0)
                {
                    string fullPath = directoryName + "\\" + theEntry.Name;
                    fullPath = fullPath.Replace("\\ ", "\\");
                    string fullDirPath = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                    FileStream streamWriter = File.Create(fullPath);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
        }
        s.Close();
        if (deleteZipFile)
        {
            File.Delete(zipPathAndFile);

        }
}

Error got:

Unhandled Exception: ICSharpCode.SharpZipLib.SharpZipBaseException: Unknown block type 6
  at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Decode () [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Inflate (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.BodyRead (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.InitialRead (System.Byte[] destination, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.CloseEntry () [0x00000] in <filename unknown>:0Error connecting stdout and stderr (127.0.0.1:10001)

  at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry () [0x00000] in <filename unknown>:0 
  at FolderNavigation.ZipController.UnZipFiles (System.String zipPathAndFile, System.String outputFolder, System.String password, Boolean deleteZipFile) [0x00119] in /Users/claudio/Projects/FolderNavigation/FolderNavigation/ZipController.cs:15 
  at FolderNavigation.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x0005e] in /Users/claudio/Projects/FolderNavigation/FolderNavigation/Main.cs:43 
  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) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31 
  at FolderNavigation.Application.Main (System.String[] args) [0x00000] in /Users/claudio/Projects/FolderNavigation/FolderNavigation/Main.cs:14 

Any idea?

Regards,
Claudio

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

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

发布评论

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

评论(1

做个少女永远怀春 2024-10-22 03:05:14

例如,您是否尝试过在 Windows 中通过 .NET 上的代码解压缩文件?也许该文件是通过 SharpZipLib 不支持的某种技术压缩的?

我的判断是,错误发生在“ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Decode”方法中,如果您检查 SharpZipLib 的源代码,则该方法只会解码压缩字节数组。

Have you tried to unzip the file via the code on .NET in windows for example? Maybe the file is zipped by some technique SharpZipLib does not support?

I'm judging by the fact that error happens in 'ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Decode' method, which if you check the source code for the SharpZipLib just decodes the array of deflated bytes.

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