只有一个图像是byte []字节,我正在尝试压缩到Zip以供用户下载,因为我可以看到字节数组保存在MemoryStream中。
问题是当我试图将MemoryStream的缓冲区读取到ImagesAux时,zippedMemoryStream.Read(ImagesAux, 0, 1000000);
不起作用,ImagesAux = zippedMemoryStream.ToArray();
这种方式已经全部有效......然后stream.Write(ImagesAux, 0, ImagesAux.Length);
通过ZipFile到SaveFileDialog无法获得ImagesAux的大小,但不是我看到的内容,ZipFile被创建但是在ALZip中不支持Format的错误,有人能指出我做错了吗?< / p>
MemoryStream zippedMemoryStream = new MemoryStream();
ZipOutputStream zipOutputStream;
zipOutputStream = new ZipOutputStream(zippedMemoryStream, 1000000);
zipOutputStream.SetLevel(0);
zipOutputStream.UseZip64 = UseZip64.On;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(ImageNameServer));//ImageNameServer
entry.DateTime = DateTime.Now;
entry.Comment = "Teste";
entry.ZipFileIndex = 1;
entry.Size = bytes.Length;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(bytes, 0, bytes.Length);
var something = zipOutputStream.GetType();
var lvl = zipOutputStream.GetLevel();
//zippedMemoryStream.Write(bytes, 0, bytes.Length);
zippedMemoryStream.Read(buffer, 0, bytes.Length);
MemoryStream auxxpto = new MemoryStream();
zippedMemoryStream.WriteTo(auxxpto);
buffer = auxxpto.ToArray();
private void DataSetDownload(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "Zip Files (*.zip)|*.zip";
dialog.DefaultExt = "zip";
dialog.DefaultFileName = "DataSet.zip";
bool? fileSelected = dialog.ShowDialog();
byte[] ImagesAux = new byte[1000000];
if (fileSelected == true)
{
var value = zippedMemoryStream.CanWrite;
var read = zippedMemoryStream.CanRead;
zippedMemoryStream.Read(ImagesAux, 0, 1000000);
ImagesAux = zippedMemoryStream.ToArray();
//ZippedFile.Read(ImagesAux, 0, ImagesAux.Length);
//ImagesAux = zippedMemoryStream.ToArray();
zipOutputStream.Finish();
zipOutputStream.Close();
using (Stream stream = dialog.OpenFile())
{
stream.Write(ImagesAux, 0, ImagesAux.Length);
//stream = zippedMemoryStream.ToArray();
//stream.Flush();
//stream.Close();
}
}
}
答案 0 :(得分:0)
此代码已从问题中删除。答案应该在答案中。
工作代码。
MemoryStream zippedMemoryStream = new MemoryStream();
ZipOutputStream zipOutputStream;
zipOutputStream = new ZipOutputStream(zippedMemoryStream, 1000000);
zipOutputStream.SetLevel(0);
zipOutputStream.UseZip64 = UseZip64.On;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(ImageNameServer));//ImageNameServer
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(bytes, 0, bytes.Length);
zipOutputStream.Finish();
zipOutputStream.CloseEntry();
private void DataSetDownload(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "Zip Files (*.zip)|*.zip";
dialog.DefaultExt = "zip";
dialog.DefaultFileName = "DataSet.zip";
bool? fileSelected = dialog.ShowDialog();
byte[] ImagesAux = new byte[1000000];
if (fileSelected == true)
{
var value = zippedMemoryStream.CanWrite;
var read = zippedMemoryStream.CanRead;
zippedMemoryStream.Position = 0;
zippedMemoryStream.Read(ImagesAux, 0, 1000000);
//ZippedFile.Read(ImagesAux, 0, ImagesAux.Length);
//ImagesAux = zippedMemoryStream.ToArray();
zipOutputStream.Finish();
zipOutputStream.Close();
using (Stream stream = dialog.OpenFile())
{
stream.Write(ImagesAux, 0, ImagesAux.Length);
//stream = zippedMemoryStream.ToArray();
//stream.Flush();
//stream.Close();
}
}
}