I'm trying to serialize an instance of a class (let's call it Car) into an xml and compress it in memory to a zip file with a single file entry in it.
I'm using the System.IO.Compression.ZipArchive class to do it:
private byte[] CompressCar(Car car)
{
using (var carStream = new MemoryStream())
using (var zipStream = new MemoryStream())
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create))
{
new XmlSerializer(typeof(Car)).Serialize(carStream, car);
ZipArchiveEntry entry = archive.CreateEntry("car.xml", CompressionLevel.Optimal);
using (var zippedFileStream = entry.Open())
{
carStream.Seek(0, SeekOrigin.Begin);
carStream.CopyTo(zippedFileStream);
}
return zipStream.ToArray();
}
}
When I save the compressed bytes to a file and later try to open it with Windows Explorer I get an error:
What am I doing wrong here?
I looked for other StackOverflow entries but I just couldn't find anything that would solve my problem. I want to compress it in memory rather than using a temporary file.
You need to dispose the ZipArchive before returning the underlying zipStream.ToArray(). E.g., you could extract the following helper method:
public static class SerializationExtensions
{
public static byte[] ToCompressedXmlZipArchive<T>(T root, string entryName)
{
using (var zipStream = new MemoryStream())
{
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create))
{
var entry = archive.CreateEntry(entryName, CompressionLevel.Optimal);
using (var zippedFileStream = entry.Open())
{
new XmlSerializer(typeof(T)).Serialize(zippedFileStream, root); // Possibly use root.GetType() instead of typeof(T)
}
}
return zipStream.ToArray();
}
}
}
And then your method becomes:
private byte[] CompressCar(Car car)
{
return SerializationExtensions.ToCompressedXmlZipArchive(car, "car.xml");
}
Related
i use BlazorInputFile on my project but dont know how to transform the stream that i get from the input file(a zipFile) to an ZipArchive to loop in it....
i see the stream is ok but when i try to make an copytoasync to a memorystream it dont work telling me the variable is not available.
So i try with an await befor the copytoasync with a async task instead of my void function loadFile, and i saw now the ms available but its empty, size is 0... seems nothing happened in the copytoasync...
private async Task loadFileAsync(IFileListEntry fileZip, ExcelWorksheet sheet2User)
{
MemoryStream mstest = new MemoryStream();
await fileZip.Data.CopyToAsync(mstest);
mstest.Position = 0;
using (ZipArchive archive = new ZipArchive(mstest, ZipArchiveMode.Update))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
//my code...
}
}
}
I had this problem too .
You can get file bytes and send that to your Api then create file again there .
in your component
public byte[] FileContent { get; set; }
protected async Task HandleAnswerFileSelected(IFileListEntry[] files)
{
foreach (var file in files)
{
FileContent = await FIleSender(file),
}
}
public async Task<byte[]> FIleSender(IFileListEntry file)
{
using (var ms = new MemoryStream())
{
await file.Data.CopyToAsync(ms);
return ms.ToArray();
}
}
in your api
//file.content is aray of byte
using var memoryStream = new MemoryStream(file.Content);
i find the solution and make it work like this, converting it to base64string and write it to a new ms!
Maybe it can help.
'''
private void LoadFile(MemoryStream msFromBlazorInputFile)
{
MemoryStream memoryfilestream = new MemoryStream(0);
memoryfilestream.Write(Convert.FromBase64String(Convert.ToBase64String(msFromBlazorInputFile.ToArray())));
using (ZipArchive archive = new ZipArchive(memoryfilestream, ZipArchiveMode.Update))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
}
}
}
'''
I'm trying to create a zip stream on the fly with some byte array data and make it download via my MVC action.
But the downloaded file always gives the following corrupted error when opened in windows.
And this error when I try to xtract from 7z
But note that the files extracted from the 7z is not corrupted.
I'm using ZipArchive and the below is my code.
private byte[] GetZippedPods(IEnumerable<POD> pods, long consignmentID)
{
using (var zipStream = new MemoryStream())
{
//Create an archive and store the stream in memory.
using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
int index = 1;
foreach (var pod in pods)
{
var zipEntry = zipArchive.CreateEntry($"POD{consignmentID}{index++}.png", CompressionLevel.NoCompression);
using (var originalFileStream = new MemoryStream(pod.ByteData))
{
using (var zipEntryStream = zipEntry.Open())
{
originalFileStream.CopyTo(zipEntryStream);
}
}
}
return zipStream.ToArray();
}
}
}
public ActionResult DownloadPOD(long consignmentID)
{
var pods = _consignmentService.GetPODs(consignmentID);
var fileBytes = GetZippedPods(pods, consignmentID);
return File(fileBytes, MediaTypeNames.Application.Octet, $"PODS{consignmentID}.zip");
}
What am I doing wrong here.
Any help would be highly appreciated as I'm struggling with this for a whole day.
Thanks in advance
Move zipStream.ToArray() outside of the zipArchive using.
The reason for your problem is that the stream is buffered. There's a few ways to deal wtih it:
You can set the stream's AutoFlush property to true.
You can manually call .Flush() on the stream.
Or, since it's MemoryStream and you're using .ToArray(), you can simply allow the stream to be Closed/Disposed first (which we've done by moving it outside the using).
I Dispose ZipArchive And error solved
public static byte[] GetZipFile(Dictionary<string, List<FileInformation>> allFileInformations)
{
MemoryStream compressedFileStream = new MemoryStream();
//Create an archive and store the stream in memory.
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
{
foreach (var fInformation in allFileInformations)
{
var files = allFileInformations.Where(x => x.Key == fInformation.Key).SelectMany(x => x.Value).ToList();
for (var i = 0; i < files.Count; i++)
{
ZipArchiveEntry zipEntry = zipArchive.CreateEntry(fInformation.Key + "/" + files[i].FileName);
var caseAttachmentModel = Encoding.UTF8.GetBytes(files[i].Content);
//Get the stream of the attachment
using (var originalFileStream = new MemoryStream(caseAttachmentModel))
using (var zipEntryStream = zipEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
//i added this line
zipArchive.Dispose();
return compressedFileStream.ToArray();
}
}
public void SaveZipFile(){
var zipFileArray = Global.GetZipFile(allFileInformations);
var zipFile = new MemoryStream(zipFileArray);
FileStream fs = new FileStream(path + "\\111.zip",
FileMode.Create,FileAccess.Write);
zipFile.CopyTo(fs);
zipFile.Flush();
fs.Close();
zipFile.Close();
}
I was also having problems with this and I found my issue was not the generation of the archive itself but rather how I was handing my GET request in AngularJS.
This post helped me: how to download a zip file using angular
The key was adding responseType: 'arraybuffer' to my $http call.
factory.serverConfigExportZIP = function () {
return $http({
url: dataServiceBase + 'serverConfigExport',
method: "GET",
responseType: 'arraybuffer'
})
};
you can remove "using" and use Dispose and Close methods
it's work for me
...
zip.Dispose();
zipStream.Close();
return zipStream.ToArray();
I know this is a C# question but for managed C++, delete the ZipArchive^ after you're done with it to fix the error.
ZipArchive^ zar = ZipFile::Open(starget, ZipArchiveMode::Create);
ZipFileExtensions::CreateEntryFromFile(zar, sfile1, "file.txt");
ZipFileExtensions::CreateEntryFromFile(zar, sfile2, "file2.txt");
delete zar;
when i wanted to create zip file directly from MemoryStream which i used for ZipArchive i was getting error ( "unexpected end of data" or zero length file )
there are three points to get ride of this error
set the last parameter of ZipArchive constructor to true ( it leaves to leave stream open after ZipArchive disposed )
call dispose() on ZipArchive and dispose it manually.
create another MemoryStream based on which you set in ZipArchive constructor, by calling ToArray() method.
here is sample code :
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create,))
{
foreach (var s3Object in objectList.S3Objects)
{
var entry = archive.CreateEntry(s3Object.Key, CompressionLevel.NoCompression);
using (var entryStream = entry.Open())
{
var request = new GetObjectRequest { BucketName = command.BucketName, Key = s3Object.Key };
using (var getObjectResponse = await client.GetObjectAsync(request))
{
await getObjectResponse.ResponseStream.CopyToAsync(entryStream);
}
}
}
archive.Dispose();
using (var fileStream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
{
var zipFileMemoryStream = new MemoryStream(memoryStream.ToArray());
zipFileMemoryStream.CopyTo(fileStream);
zipFileMemoryStream.Flush();
fileStream.Close();
zipFileMemoryStream.Close();
}
}
}
I had the same problem... In this case I just needed to move the ToArray() (byte[]) from MemoryStream outside the using (var zipArchive = new ZipArchive...
I think it is necessary for using related to ZipArchive to completely close and dispose of the file before converting it into a byte array
i have 2 zip files (zip1 and zip2), i need merge this files in one, how can i solve it?
I understand that I can fix it decompressing ZIP1 to a temp folder and then add it to zip2, but i think it is inefficient, what is the faster method?
I'm using System.IO.Compression library...
I use this code to explore Zip2:
using (ZipArchive archive = ZipFile.OpenRead(Zip2))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
/*...*/
}
}
PD/: The Zip1 and Zip2 files have FILES, FOLDERS AND SUBFOLDERS
I haven't really tested this, but you can give it a try:
public ZipArchive Merge(List<ZipArchive> archives)
{
if (archives == null) throw new ArgumentNullException("archives");
if (archives.Count == 1) return archives.Single();
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var zipArchive in archives)
{
foreach (var zipArchiveEntry in zipArchive.Entries)
{
var file = archive.CreateEntry(zipArchiveEntry.FullName);
using (var entryStream = file.Open()) {
using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write(zipArchiveEntry.Open()); }
}
}
}
return archive;
}
}
}
I had an equivalent problem. The zip files came from another process as byte arrays. This method adds their contents to an existing ZipArchive.
using System.IO;
using System.IO.Compression;
/// <summary>
/// Copies the contents of a zip "file" represented as a byte array
/// to the given ZipArchive.
/// </summary>
/// <param name="targetArchive">The archive to which to add the entries.</param>
/// <param name="zippedSourceBuffer">The source data as zip "file" in a byte array.</param>
public static void addZipBufferToZipArchive(ZipArchive targetArchive, byte[] zippedSourceBuffer)
{
using (MemoryStream zippedSourceStream = new MemoryStream(zippedSourceBuffer))
{
using (ZipArchive sourceArchive = new ZipArchive(zippedSourceStream))
{
foreach (ZipArchiveEntry sourceEntry in sourceArchive.Entries)
{
ZipArchiveEntry targetEntry = targetArchive.CreateEntry(sourceEntry.FullName);
using (Stream targetEntryStream = targetEntry.Open())
{
using (Stream sourceStream = sourceEntry.Open())
{
sourceStream.CopyTo(targetEntryStream);
}
}
}
}
}
}
There does not appear to be any way to directly move a ZipArchiveEntry from one ZipArchive to another, so I resorted to unpacking the source and repacking it to the target via the copy operation.
If you already have a ZipArchive as source, you wouldn't need the MemoryStream zippedSourceStream and could modify the code to use sourceArchive directly as the method parameter.
This example shows how to merge two zip files into third one in memory or with actual files with the help of DotNetZip library:
//file1 and file2 are MemoryStream or file names
//for performance reason it is better that file1 is less than file2
//file1.Position = 0;//ensure at case of MemoryStream
using (var zip1 = ZipFile.Read(file1))
{
//file2.Position = 0;//ensure at case of MemoryStream
using (var zip2 = ZipFile.Read(file2))
{
var zip2EntryNames = zip2.Entries.Select(y => y.FileName).ToList();
foreach (var entry in zip1.Entries.Where(x => !zip2EntryNames.Contains(x.FileName)).ToList())
{
var memoryStream = new MemoryStream();
entry.Extract(memoryStream);
memoryStream.Position = 0;
zip2.AddEntry(entry.FileName, memoryStream);
}
if(save2file)
zip2.Save(fileNameResult);
else
using (var result = new MemoryStream())
{
zip2.Save(result);
}
}
}
I have files (from 3rd parties) that are being FTP'd to a directory on our server. I download them and process them even 'x' minutes. Works great.
Now, some of the files are .zip files. Which means I can't process them. I need to unzip them first.
FTP has no concept of zip/unzipping - so I'll need to grab the zip file, unzip it, then process it.
Looking at the MSDN zip api, there seems to be no way i can unzip to a memory stream?
So is the only way to do this...
Unzip to a file (what directory? need some -very- temp location ...)
Read the file contents
Delete file.
NOTE: The contents of the file are small - say 4k <-> 1000k.
Zip compression support is built in:
using System.IO;
using System.IO.Compression;
// ^^^ requires a reference to System.IO.Compression.dll
static class Program
{
const string path = ...
static void Main()
{
using(var file = File.OpenRead(path))
using(var zip = new ZipArchive(file, ZipArchiveMode.Read))
{
foreach(var entry in zip.Entries)
{
using(var stream = entry.Open())
{
// do whatever we want with stream
// ...
}
}
}
}
}
Normally you should avoid copying it into another stream - just use it "as is", however, if you absolutely need it in a MemoryStream, you could do:
using(var ms = new MemoryStream())
{
stream.CopyTo(ms);
ms.Position = 0; // rewind
// do something with ms
}
You can use ZipArchiveEntry.Open to get a stream.
This code assumes the zip archive has one text file.
using (FileStream fs = new FileStream(path, FileMode.Open))
using (ZipArchive zip = new ZipArchive(fs) )
{
var entry = zip.Entries.First();
using (StreamReader sr = new StreamReader(entry.Open()))
{
Console.WriteLine(sr.ReadToEnd());
}
}
using (ZipArchive archive = new ZipArchive(webResponse.GetResponseStream()))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
Stream s = entry.Open();
var sr = new StreamReader(s);
var myStr = sr.ReadToEnd();
}
}
Looks like here is what you need:
using (var za = ZipFile.OpenRead(path))
{
foreach (var entry in za.Entries)
{
using (var r = new StreamReader(entry.Open()))
{
//your code here
}
}
}
You can use SharpZipLib among a variety of other libraries to achieve this.
You can use the following code example to unzip to a MemoryStream, as shown on their wiki:
using ICSharpCode.SharpZipLib.Zip;
// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
//
public MemoryStream 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[] byteArrayOut = outputMemStream.GetBuffer();
long len = outputMemStream.Length;
}
Ok so combining all of the above, suppose you want to in a very simple way take a zip file called
"file.zip" and extract it to "C:\temp" folder. (Note: This example was only tested for compress text files) You may need to do some modifications for binary files.
using System.IO;
using System.IO.Compression;
static void Main(string[] args)
{
//Call it like this:
Unzip("file.zip",#"C:\temp");
}
static void Unzip(string sourceZip, string targetPath)
{
using (var z = ZipFile.OpenRead(sourceZip))
{
foreach (var entry in z.Entries)
{
using (var r = new StreamReader(entry.Open()))
{
string uncompressedFile = Path.Combine(targetPath, entry.Name);
File.WriteAllText(uncompressedFile,r.ReadToEnd());
}
}
}
}
I create a zip file in a controller from a byte array and I return the zip file as a fileresult. When I download the zip File and extract the file, it is corrupt. I'm doing it this way:
byte[] fileBytes =array
MemoryStream fileStream = new MemoryStream(fileBytes);
MemoryStream outputStream = new MemoryStream();
fileStream.Seek(0, SeekOrigin.Begin);
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddEntry(returnFileName, fileStream);
zipFile.Save(outputStream);
}
outputStream.Position = 0;
FileStreamResult fileResult = new FileStreamResult(outputStream, System.Net.Mime.MediaTypeNames.Application.Zip);
fileResult.FileDownloadName = returnFileName + ".zip";
return fileResult;
You might be unlucky hitting one of the open bugs in DotNetZip. There is e.g. an issue depending on the file size (https://dotnetzip.codeplex.com/workitem/14087).
Unfortunately, DotNetZip has some critical issues and the project seems no longer be actively be maintained. Better alternatives would be to use SharpZipLib (if you comply with their GPL-based license), or one of the .NET ports of zlib.
If you are on .NET 4.5 you can use the built-in classes in the System.IO.Compression namespace. The following sample can be found in the documentation of the ZipArchive class:
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (var zipToOpen =
new FileStream(#"c:\tmp\release.zip", FileMode.Open))
{
using (var archive =
new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
var readmeEntry = archive.CreateEntry("Readme.txt");
using (var writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
}
}
}
}
}
public class HomeController : Controller
{
public FileResult Index()
{
FileStreamResult fileResult = new FileStreamResult(GetZippedStream(), System.Net.Mime.MediaTypeNames.Application.Zip);
fileResult.FileDownloadName = "result" + ".zip";
return fileResult;
}
private static Stream GetZippedStream()
{
byte[] fileBytes = Encoding.ASCII.GetBytes("abc");
string returnFileName = "something";
MemoryStream fileStream = new MemoryStream(fileBytes);
MemoryStream resultStream = new MemoryStream();
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddEntry(returnFileName, fileStream);
zipFile.Save(resultStream);
}
resultStream.Position = 0;
return resultStream;
}
}