Crear un archivo ZIP en la memoria usando System.IO.Compression

Intento crear un archivo ZIP con un archivo de texto de demostración simple usando un MemoryStream siguiente manera:

 using (var memoryStream = new MemoryStream()) using (var archive = new ZipArchive(memoryStream , ZipArchiveMode.Create)) { var demoFile = archive.CreateEntry("foo.txt"); using (var entryStream = demoFile.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write("Bar!"); } using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create)) { stream.CopyTo(fileStream); } } 

Si ejecuto este código, se crea el archivo de almacenamiento pero foo.txt no lo está.

Sin embargo, si reemplazo el MemoryStream directamente con la secuencia de archivos, el archivo se crea correctamente:

 using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create)) using (var archive = new ZipArchive(fileStream, FileMode.Create)) { // ... } 

¿Es posible usar un MemoryStream para crear el archivo ZIP sin FileStream ?

    Gracias a https://stackoverflow.com/a/12350106/222748 obtuve:

     using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var demoFile = archive.CreateEntry("foo.txt"); using (var entryStream = demoFile.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write("Bar!"); } } using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } 

    Entonces, debemos llamar a disponer de ZipArchive antes de que podamos usarlo, lo que significa pasar ‘verdadero’ como el tercer parámetro al ZipArchive para que podamos seguir accediendo a la transmisión después de eliminarlo.

    Solo otra versión de compresión sin escribir ningún archivo.

     string fileName = "export_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"; byte[] fileBytes = here is your file in bytes byte[] compressedBytes; string fileNameZip = "Export_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip"; using (var outStream = new MemoryStream()) { using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true)) { var fileInArchive = archive.CreateEntry(fileName, CompressionLevel.Optimal); using (var entryStream = fileInArchive.Open()) using (var fileToCompressStream = new MemoryStream(fileBytes)) { fileToCompressStream.CopyTo(entryStream); } } compressedBytes = outStream.ToArray(); } 

    Establezca la posición de la secuencia en el 0 antes de copiarla en la secuencia zip.

     using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var demoFile = archive.CreateEntry("foo.txt"); using (var entryStream = demoFile.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write("Bar!"); } } using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create)) { memoryStream.Position=0; memoryStream.WriteTo(fileStream); } } 

    Debe terminar de escribir la secuencia de la memoria y luego volver a leer el búfer.

      using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create)) { var demoFile = archive.CreateEntry("foo.txt"); using (var entryStream = demoFile.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write("Bar!"); } } using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create)) { var bytes = memoryStream.GetBuffer(); fileStream.Write(bytes,0,bytes.Length ); } } 

    Esta es la forma de convertir una entidad a un archivo XML y luego comprimirlo:

     private void downloadFile(EntityXML xml) { string nameDownloadXml = "File_1.xml"; string nameDownloadZip = "File_1.zip"; var serializer = new XmlSerializer(typeof(EntityXML)); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("content-disposition", "attachment;filename=" + nameDownloadZip); using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var demoFile = archive.CreateEntry(nameDownloadXml); using (var entryStream = demoFile.Open()) using (StreamWriter writer = new StreamWriter(entryStream, System.Text.Encoding.UTF8)) { serializer.Serialize(writer, xml); } } using (var fileStream = Response.OutputStream) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } Response.End(); 

    }

     using System; using System.IO; using System.IO.Compression; namespace ConsoleApplication { class Program`enter code here` { static void Main(string[] args) { using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) { ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt"); using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) { writer.WriteLine("Information about this package."); writer.WriteLine("========================"); } } } } } } 

    Solución de trabajo para MVC

      public ActionResult Index() { string fileName = "test.pdf"; string fileName1 = "test.vsix"; string fileNameZip = "Export_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip"; byte[] fileBytes = System.IO.File.ReadAllBytes(@"C:\test\test.pdf"); byte[] fileBytes1 = System.IO.File.ReadAllBytes(@"C:\test\test.vsix"); byte[] compressedBytes; using (var outStream = new MemoryStream()) { using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true)) { var fileInArchive = archive.CreateEntry(fileName, CompressionLevel.Optimal); using (var entryStream = fileInArchive.Open()) using (var fileToCompressStream = new MemoryStream(fileBytes)) { fileToCompressStream.CopyTo(entryStream); } var fileInArchive1 = archive.CreateEntry(fileName1, CompressionLevel.Optimal); using (var entryStream = fileInArchive1.Open()) using (var fileToCompressStream = new MemoryStream(fileBytes1)) { fileToCompressStream.CopyTo(entryStream); } } compressedBytes = outStream.ToArray(); } return File(compressedBytes, "application/zip", fileNameZip); } 
      private void button6_Click(object sender, EventArgs e) { //create With Input FileNames AddFileToArchive_InputByte(new ZipItem[]{ new ZipItem( @"E:\b\1.jpg",@"images\1.jpg"), new ZipItem(@"E:\b\2.txt",@"text\2.txt")}, @"C:\test.zip"); //create with input stream AddFileToArchive_InputByte(new ZipItem[]{ new ZipItem(File.ReadAllBytes( @"E:\b\1.jpg"),@"images\1.jpg"), new ZipItem(File.ReadAllBytes(@"E:\b\2.txt"),@"text\2.txt")}, @"C:\test.zip"); //Create Archive And Return StreamZipFile MemoryStream GetStreamZipFile = AddFileToArchive(new ZipItem[]{ new ZipItem( @"E:\b\1.jpg",@"images\1.jpg"), new ZipItem(@"E:\b\2.txt",@"text\2.txt")}); //Extract in memory ZipItem[] ListitemsWithBytes = ExtractItems(@"C:\test.zip"); //Choese Files For Extract To memory List ListFileNameForExtract = new List(new string[] { @"images\1.jpg", @"text\2.txt" }); ListitemsWithBytes = ExtractItems(@"C:\test.zip", ListFileNameForExtract); // Choese Files For Extract To Directory ExtractItems(@"C:\test.zip", ListFileNameForExtract, "c:\\extractFiles"); } public struct ZipItem { string _FileNameSource; string _PathinArchive; byte[] _Bytes; public ZipItem(string __FileNameSource, string __PathinArchive) { _Bytes=null ; _FileNameSource = __FileNameSource; _PathinArchive = __PathinArchive; } public ZipItem(byte[] __Bytes, string __PathinArchive) { _Bytes = __Bytes; _FileNameSource = ""; _PathinArchive = __PathinArchive; } public string FileNameSource { set { FileNameSource = value; } get { return _FileNameSource; } } public string PathinArchive { set { _PathinArchive = value; } get { return _PathinArchive; } } public byte[] Bytes { set { _Bytes = value; } get { return _Bytes; } } } public void AddFileToArchive(ZipItem[] ZipItems, string SeveToFile) { MemoryStream memoryStream = new MemoryStream(); //Create Empty Archive ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); foreach (ZipItem item in ZipItems) { //Create Path File in Archive ZipArchiveEntry FileInArchive = archive.CreateEntry(item.PathinArchive); //Open File in Archive For Write var OpenFileInArchive = FileInArchive.Open(); //Read Stream FileStream fsReader = new FileStream(item.FileNameSource, FileMode.Open, FileAccess.Read); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 0; while (fsReader.Position != fsReader.Length) { //Read Bytes ReadByte = fsReader.Read(ReadAllbytes, 0, ReadAllbytes.Length); //Write Bytes OpenFileInArchive.Write(ReadAllbytes, 0, ReadByte); } fsReader.Dispose(); OpenFileInArchive.Close(); } archive.Dispose(); using (var fileStream = new FileStream(SeveToFile, FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } public MemoryStream AddFileToArchive(ZipItem[] ZipItems) { MemoryStream memoryStream = new MemoryStream(); //Create Empty Archive ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); foreach (ZipItem item in ZipItems) { //Create Path File in Archive ZipArchiveEntry FileInArchive = archive.CreateEntry(item.PathinArchive); //Open File in Archive For Write var OpenFileInArchive = FileInArchive.Open(); //Read Stream FileStream fsReader = new FileStream(item.FileNameSource, FileMode.Open, FileAccess.Read); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 0; while (fsReader.Position != fsReader.Length) { //Read Bytes ReadByte = fsReader.Read(ReadAllbytes, 0, ReadAllbytes.Length); //Write Bytes OpenFileInArchive.Write(ReadAllbytes, 0, ReadByte); } fsReader.Dispose(); OpenFileInArchive.Close(); } archive.Dispose(); return memoryStream; } public void AddFileToArchive_InputByte(ZipItem[] ZipItems, string SeveToFile) { MemoryStream memoryStream = new MemoryStream(); //Create Empty Archive ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); foreach (ZipItem item in ZipItems) { //Create Path File in Archive ZipArchiveEntry FileInArchive = archive.CreateEntry(item.PathinArchive); //Open File in Archive For Write var OpenFileInArchive = FileInArchive.Open(); //Read Stream // FileStream fsReader = new FileStream(item.FileNameSource, FileMode.Open, FileAccess.Read); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 4096 ;int TotalWrite=0; while (TotalWrite != item.Bytes.Length) { if(TotalWrite+4096>item.Bytes.Length) ReadByte=item.Bytes.Length-TotalWrite; Array.Copy(item.Bytes, TotalWrite, ReadAllbytes, 0, ReadByte); //Write Bytes OpenFileInArchive.Write(ReadAllbytes, 0, ReadByte); TotalWrite += ReadByte; } OpenFileInArchive.Close(); } archive.Dispose(); using (var fileStream = new FileStream(SeveToFile, FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } public MemoryStream AddFileToArchive_InputByte(ZipItem[] ZipItems) { MemoryStream memoryStream = new MemoryStream(); //Create Empty Archive ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); foreach (ZipItem item in ZipItems) { //Create Path File in Archive ZipArchiveEntry FileInArchive = archive.CreateEntry(item.PathinArchive); //Open File in Archive For Write var OpenFileInArchive = FileInArchive.Open(); //Read Stream // FileStream fsReader = new FileStream(item.FileNameSource, FileMode.Open, FileAccess.Read); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 4096 ;int TotalWrite=0; while (TotalWrite != item.Bytes.Length) { if(TotalWrite+4096>item.Bytes.Length) ReadByte=item.Bytes.Length-TotalWrite; Array.Copy(item.Bytes, TotalWrite, ReadAllbytes, 0, ReadByte); //Write Bytes OpenFileInArchive.Write(ReadAllbytes, 0, ReadByte); TotalWrite += ReadByte; } OpenFileInArchive.Close(); } archive.Dispose(); return memoryStream; } public void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName) { //Opens the zip file up to be read using (ZipArchive archive = ZipFile.OpenRead(sourceArchiveFileName)) { if (Directory.Exists(destinationDirectoryName)==false ) Directory.CreateDirectory(destinationDirectoryName); //Loops through each file in the zip file archive.ExtractToDirectory(destinationDirectoryName); } } public void ExtractItems(string sourceArchiveFileName,List< string> _PathFilesinArchive, string destinationDirectoryName) { //Opens the zip file up to be read using (ZipArchive archive = ZipFile.OpenRead(sourceArchiveFileName)) { //Loops through each file in the zip file foreach (ZipArchiveEntry file in archive.Entries) { int PosResult = _PathFilesinArchive.IndexOf(file.FullName); if (PosResult != -1) { //Create Folder if (Directory.Exists( destinationDirectoryName + "\\" +Path.GetDirectoryName( _PathFilesinArchive[PosResult])) == false) Directory.CreateDirectory(destinationDirectoryName + "\\" + Path.GetDirectoryName(_PathFilesinArchive[PosResult])); Stream OpenFileGetBytes = file.Open(); FileStream FileStreamOutput = new FileStream(destinationDirectoryName + "\\" + _PathFilesinArchive[PosResult], FileMode.Create); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 0; int TotalRead = 0; while (TotalRead != file.Length) { //Read Bytes ReadByte = OpenFileGetBytes.Read(ReadAllbytes, 0, ReadAllbytes.Length); TotalRead += ReadByte; //Write Bytes FileStreamOutput.Write(ReadAllbytes, 0, ReadByte); } FileStreamOutput.Close(); OpenFileGetBytes.Close(); _PathFilesinArchive.RemoveAt(PosResult); } if (_PathFilesinArchive.Count == 0) break; } } } public ZipItem[] ExtractItems(string sourceArchiveFileName) { List< ZipItem> ZipItemsReading = new List(); //Opens the zip file up to be read using (ZipArchive archive = ZipFile.OpenRead(sourceArchiveFileName)) { //Loops through each file in the zip file foreach (ZipArchiveEntry file in archive.Entries) { Stream OpenFileGetBytes = file.Open(); MemoryStream memstreams = new MemoryStream(); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 0; int TotalRead = 0; while (TotalRead != file.Length) { //Read Bytes ReadByte = OpenFileGetBytes.Read(ReadAllbytes, 0, ReadAllbytes.Length); TotalRead += ReadByte; //Write Bytes memstreams.Write(ReadAllbytes, 0, ReadByte); } memstreams.Position = 0; OpenFileGetBytes.Close(); memstreams.Dispose(); ZipItemsReading.Add(new ZipItem(memstreams.ToArray(),file.FullName)); } } return ZipItemsReading.ToArray(); } public ZipItem[] ExtractItems(string sourceArchiveFileName,List< string> _PathFilesinArchive) { List< ZipItem> ZipItemsReading = new List(); //Opens the zip file up to be read using (ZipArchive archive = ZipFile.OpenRead(sourceArchiveFileName)) { //Loops through each file in the zip file foreach (ZipArchiveEntry file in archive.Entries) { int PosResult = _PathFilesinArchive.IndexOf(file.FullName); if (PosResult!= -1) { Stream OpenFileGetBytes = file.Open(); MemoryStream memstreams = new MemoryStream(); byte[] ReadAllbytes = new byte[4096];//Capcity buffer int ReadByte = 0; int TotalRead = 0; while (TotalRead != file.Length) { //Read Bytes ReadByte = OpenFileGetBytes.Read(ReadAllbytes, 0, ReadAllbytes.Length); TotalRead += ReadByte; //Write Bytes memstreams.Write(ReadAllbytes, 0, ReadByte); } //Create item ZipItemsReading.Add(new ZipItem(memstreams.ToArray(),file.FullName)); OpenFileGetBytes.Close(); memstreams.Dispose(); _PathFilesinArchive.RemoveAt(PosResult); } if (_PathFilesinArchive.Count == 0) break; } } return ZipItemsReading.ToArray(); }