파일이 생성되고 크기가 괜찮은 것 같지만 두 번 클릭하면 형식이 잘못되었거나 파일이 손상되었다고 표시됩니다. dotnetzip 라이브러리를 사용하여 스트림을 압축하는 동안 손상된 파일
이
내가 호출 방법SaveStreamToFile(documentCompressedName,getDocument());
의 getDocument (에 그런
public MemoryStream CompressFiles(Dictionary<string, MemoryStream> filesToBeCompressed)
{
var output = new MemoryStream();
using (var zip = new ZipFile())
{
foreach (var entry in filesToBeCompressed)
{
entry.Value.Seek(0, SeekOrigin.Begin); // <-- must do this after writing the stream (I've read this in a blog
zip.AddEntry(entry.Key.Substring(entry.Key.LastIndexOf('/') + 1, entry.Key.Length - entry.Key.LastIndexOf('/') - 1), entry.Value);
zip.Save(output);
}
}
return output;
}
을 사용하고 코드) 압축 내부적으로
그리고 그 메소드를 호출 드디어
private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
if (stream.Length == 0) return;
// Create a FileStream object to write a stream to a file
using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
{
// Fill the bytes[] array with the stream data
var bytesInStream = new byte[stream.Length];
stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
// Use FileStream object to write to the specified file
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
}
아이디어? 미리 감사드립니다. 기예르모.
감사합니다. 너는 옳았다! – polonskyg