gpt4 book ai didi

c# - 使用 DotNetZip 将文件从 Base64 解压缩为字符串

转载 作者:行者123 更新时间:2023-11-30 16:59:55 27 4
gpt4 key购买 nike

当我尝试解压缩代码中的文件时,出现以下错误。

   at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)
at System.IO.StreamReader..ctor(Stream stream)
at Arkle.AzureServiceBusSample.MessageFetcher.ConvertBase64ZippedStringToNormalString(String stringBase64Zipped) in c:\CodeTfsArklePosBenLenovo\Arkle.AzureServiceBusSample\Arkle.AzureServiceBusSample\MessageFetcher.cs:line 171
at Arkle.AzureServiceBusSample.MessageFetcher.DownloadMessages(String username, String password, String identifier) in c:\CodeTfsArklePosBenLenovo\Arkle.AzureServiceBusSample\Arkle.AzureServiceBusSample\MessageFetcher.cs:line 92
at Arkle.AzureServiceBusSample.MessageFetcher.ReceiveMessages(String username, String password, String endPoint, String subscriptionName) in c:\CodeTfsArklePosBenLenovo\Arkle.AzureServiceBusSample\Arkle.AzureServiceBusSample\MessageFetcher.cs:line 48

System.ArgumentException:流不可读。在 System.IO.StreamReader..ctor(Stream stream, Encoding 编码, BooleandetectEncodingFromByteOrderMarks,|nt32 bufferSize, bool 值|eaveOpen)在 System.IO.StreamReader..ctor(流流)

它发生在行 var streamReader1 = new StreamReader(ms); 我已经检查过了,就在上面我能够将文件保存到磁盘并且它被正确解压缩。但是我需要将它直接解压缩到内存流中。

我使用的是 DotNetZip 版本 1.9.2。

我的代码:

    using ionic.zip;

.............
public static string UnzipToString(string stringBase64Zipped)
{
var bytesZipFile = Convert.FromBase64String(stringBase64Zipped);

var msZipFile = new MemoryStream(bytesZipFile);
msZipFile.Position = 0;
using (var zipFile1 = ZipFile.Read(msZipFile))
{
zipFile1.Save(string.Format(@"C:\Temp\{0}.zip", Guid.NewGuid())); // This works
var zipEntry1 = zipFile1.Entries.First();

using (var ms = new MemoryStream())
{
ms.Position = 0;
zipEntry1.Extract(ms);
var streamReader1 = new StreamReader(ms);
var result = String.Empty;
ms.Position = 0;
result = streamReader1.ReadToEnd();

return result;
}
}
}

Line and error when it stopz

当我尝试按照下面的建议将 memeorystream 位置设置为 0 时,我现在遇到了一个新错误。

System.ObjectDisposedException: Cannot access a closed Stream.

at System.IO.__Error.StreamIsClosed()

at System.IO.MemoryStream.set_Position(Int64 value)

at Arkle.AzureServiceBusSample.MessageFetcher.ConvertBase64ZippedStringToNormalString(String stringBase64Zipped) in c:\CodeTfsArklePosBenLenovo\Arkle.AzureServiceBusSample\Arkle.AzureServiceBusSample\MessageFetcher.cs:line 171

at Arkle.AzureServiceBusSample.MessageFetcher.DownloadMessages(String username, String password, String identifier) in c:\CodeTfsArklePosBenLenovo\Arkle.AzureServiceBusSample\Arkle.AzureServiceBusSample\MessageFetcher.cs:line 92

at Arkle.AzureServiceBusSample.MessageFetcher.ReceiveMessages(String username, String password, String endPoint, String subscriptionName) in c:\CodeTfsArklePosBenLenovo\Arkle.AzureServiceBusSample\Arkle.AzureServiceBusSample\MessageFetcher.cs:line 48

最佳答案

不确定您要实现什么。这是我的示例代码,可以正常工作

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var bytes = File.ReadAllBytes(@"e:\scripts_04-10-2014.zip");
var zipAsBase64 = Convert.ToBase64String(bytes);

foreach (var item in GetUnzippedItemsAsStream(zipAsBase64))
{
using (var fs = new FileStream(Path.Combine(@"e:\", item.Item1), FileMode.CreateNew,
FileAccess.ReadWrite))
{
item.Item2.CopyTo(fs);
}
}
}

public IEnumerable<Tuple<string, Stream>> GetUnzippedItemsAsStream(string stringBase64Zipped)
{
var bytesZipFile = Convert.FromBase64String(stringBase64Zipped);
using (var ms = new MemoryStream(bytesZipFile))
{
using (var zipFile = ZipFile.Read(ms))
{
foreach (var zipEntry in zipFile.Entries)
{
var outputStream = new MemoryStream();
zipEntry.Extract(outputStream);
yield return new Tuple<string, Stream>(zipEntry.FileName, new MemoryStream(outputStream.ToArray()));
}
}
}
}

请注意以下代码

  zipEntry.Extract(outputStream);
yield return new Tuple<string, Stream>(zipEntry.FileName, new MemoryStream(outputStream.ToArray()));

我需要用 old.ToArray() ( which works even with closed MemoryStreams ) 创建新的 MemoryStream 因为有一个 known issue在当前的 DotNetZipLib 中关于在提取后关闭流。

关于c# - 使用 DotNetZip 将文件从 Base64 解压缩为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com