C# 将Zip文件转换为字节[],将字节[]转换为Zip文件
我尝试将zip文件转换为byte[]并将其写入文本文件C# 将Zip文件转换为字节[],将字节[]转换为Zip文件,c#,file,C#,File,我尝试将zip文件转换为byte[]并将其写入文本文件 int BufferSize=65536; private void button1_Click(object sender, EventArgs e) { DialogResult re = openFileDialog1.ShowDialog(); if (re == DialogResult.OK) { string fileName = openFileDialog1.FileName;
int BufferSize=65536;
private void button1_Click(object sender, EventArgs e)
{
DialogResult re = openFileDialog1.ShowDialog();
if (re == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
try
{
byte[] bytes = File.ReadAllBytes(fileName);
File.WriteAllBytes(@"F:\Info.txt", bytes);
}
catch (Exception) { }
}
}
然后我尝试将这些字节转换为zip文件。但我做不到
我的代码在这里:
private void button2_Click(object sender, EventArgs e)
{
DialogResult re = openFileDialog1.ShowDialog();
if (re == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
try
{
byte[] bytes = File.ReadAllBytes(fileName);
using (var mstrim = new MemoryStream(bytes))
{
using (var inStream = new GZipStream(mstrim, CompressionMode.Compress))
{
using (var outStream = File.Create("Tax.Zip"))
{
var buffer = new byte[BufferSize];
int readBytes;
while ((readBytes = inStream.Read(buffer, 0, BufferSize)) != 0)
{
outStream.Write(buffer, 0, readBytes);
}
}
}
}
}
catch (Exception) { }
}
}
错误:文件模式无效
需要什么文件模式以及如何完成我所描述的内容?您使用的是GZipStream,它用于GZip文件,而不是PK Zip文件。这显然行不通。请尝试ZipFile类,尽管遗憾的是,它不适用于流,只适用于文件
除了只是一种不同的文件格式外,最大的区别在于GZip仅用于压缩,而Zip也是一种存档,即它可以包含多个文件。您使用的是GZipStream,它用于GZip文件,而不是PK Zip文件。这显然行不通。请尝试ZipFile类,尽管遗憾的是,它不适用于流,只适用于文件
除了只是一种不同的文件格式外,最大的区别在于GZip仅用于压缩,而Zip也是一种存档,即它可以包含多个文件。试试这个
private void button1_Click(object sender, EventArgs e)
{
byte[] arr;
MemoryStream ms = new MemoryStream();
arr = File.ReadAllBytes("C:\\asik.zip");
File.WriteAllBytes(@"D:\\asik.txt", arr);
ms.Close();
FileStream stream = File.OpenRead(@"D:\\asik.txt");
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
MemoryStream ms1 = new MemoryStream(fileBytes);
CreateToMemoryStream(ms1, @"D:\\asik.zip");
ms1.Close();
}
public void CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName)
{
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
ZipEntry newEntry = new ZipEntry(zipEntryName);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
//outputMemStream.Position = 0;
//return outputMemStream;
//// Alternative outputs:
//// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
//byte[] byteArrayOut = outputMemStream.ToArray();
//// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
//byte[] byteArrayOut2 = outputMemStream.GetBuffer();
//long len = outputMemStream.Length;
}
试试这个
private void button1_Click(object sender, EventArgs e)
{
byte[] arr;
MemoryStream ms = new MemoryStream();
arr = File.ReadAllBytes("C:\\asik.zip");
File.WriteAllBytes(@"D:\\asik.txt", arr);
ms.Close();
FileStream stream = File.OpenRead(@"D:\\asik.txt");
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
MemoryStream ms1 = new MemoryStream(fileBytes);
CreateToMemoryStream(ms1, @"D:\\asik.zip");
ms1.Close();
}
public void CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName)
{
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
ZipEntry newEntry = new ZipEntry(zipEntryName);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
//outputMemStream.Position = 0;
//return outputMemStream;
//// Alternative outputs:
//// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
//byte[] byteArrayOut = outputMemStream.ToArray();
//// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
//byte[] byteArrayOut2 = outputMemStream.GetBuffer();
//long len = outputMemStream.Length;
}
试试这个
byte[] data = File.ReadAllBytes("D:\\z.7z");
File.WriteAllBytes("D:\\t.txt", data); // Requires System.IO
byte[] newdata = File.ReadAllBytes("D:\\t.txt");
File.WriteAllBytes("D:\\a.7z", newdata); // Requires System.IO
试试这个
byte[] data = File.ReadAllBytes("D:\\z.7z");
File.WriteAllBytes("D:\\t.txt", data); // Requires System.IO
byte[] newdata = File.ReadAllBytes("D:\\t.txt");
File.WriteAllBytes("D:\\a.7z", newdata); // Requires System.IO
这种缩进样式确实没有帮助。按Ctrl-K,D。实际上,您必须解压缩zip文件。从zip文件中读取字节不会提供文件中已压缩的实际数据。这就是为什么不能从刚才读取的字节重新创建zip文件的原因。使用DotNetZIP:可以在比Gzip更少的时间和精力内完成更多的工作这种缩进样式确实没有帮助。按Ctrl-K,D。实际上,您必须解压缩zip文件。从zip文件中读取字节不会提供文件中已压缩的实际数据。这就是为什么不能从刚才读取的字节重新创建zip文件的原因。使用DotNetZIP:可以在比Gzip更少的时间和精力内完成更多的工作通过扩展异常类创建一个名为MoreZipeException的自定义异常类请编辑答案并为您提供的代码添加解释。在doc文件夹中只有一个.zip文件,否则它将引发异常,如果有一个.zip文件,它将为您提供字节[]通过扩展异常类创建一个名为MoreZipeException的自定义异常类请编辑答案并对您提供的代码添加解释。在doc文件夹中,将只有一个.zip文件,否则它将引发异常,如果有一个.zip文件,它将为您提供该文件的字节[]。