I ZipArchive of .NET 4.5 and it is working correctly FileStream :
public static byte [] CompressWithFiles (string dir) {var archiveName = Path.Combine (Path.GetTempPath (), Guid.NewGuid () toString () + "ZIP".) ; Var file = directory. Gatecams (testsConsoleDir); (Var stream = new FileStream (archiveName, FileMode.Create)) Using using {(var collection = new ZipArchive (stream, ZipArchiveMode.Create)) {foreach (var file files) archive.CreateEntryFromFile (File, Path .GetFileName (file), compression level. Optical); }} Byte [] results = file. Read AllBytes (archiveName); Return result; } But if I'm trying to compress files from MemoryStream , it returns a bytes array that can not be saved to disk:
Public Static Byte [] Compression Medium (String Dior) {var files = Directory.GetFiles (dir); Byte [] results = null; (VAR using section = Using new MemoryStream ()) {(var collection = new ZipArchive (stream, ZipArchiveMode.Create)) {foreach (var file files) ArchivekCreateEntryFromFile (file, Path.GetFileName (file) , CompressionLevel.Optimal); section. Composition = 0; Result = new byte [stream.Length]; Stream Read (Results, 0, (Stream) length); }} Return result; } I compared the arrays in the first and second ways and found that they are equal to the fact that there are remaining bytes at the end of the first array, which is to open the zip archive How can I fix this problem in the CompressInMemory method? I'm trying to avoid data stored on the disk.
You have to "close" before reading it:
using (var collection = new ZipArchive (stream, ZipArchiveMode.Create, true)) {foreach (var file files) archive.CreateEntryFromFile (file, Path.GetFileName (file), CompressionLevel .Optimal); } Stream.Position = 0; Result = new byte [stream.Length]; Stream Read (Results, 0, (Stream) length); // Note that you can replace the last 3 rows with the stream. Page = 0 further) with: // result = stream.ToArray (); But you have to set "code-zipArchive to stop the stream" non-disposed "(last true ), when it This way the zip has been "flushed" in the stream. If you notice that you correct it in the FileStream example
P> In general, you should stream the stream.Flush () first .page = 0 , because in stream There may be some unwanted internal buffers, But does MemoryStream it is not necessary, because
override the stream. Flush method do
No comments:
Post a Comment