Moving file directly into zip file - c#

I'm trying move Source_File.mp4 into Destination_Zip.zip directly. I'm currently trying it by creating a new entry Transfer.mp4 and copying the bytes over. The code runs to the end but the file is never added to the ZIP file. I'm not sure if I'm missing something or if this isn't possible.
string sourceFile = basePath + "Source_File.mp4";
string destinationZip = basePath + "Desintation_Zip.zip";
using (var file = File.OpenRead(destinationZip))
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(file.Length);
file.Read(memoryStream.GetBuffer(), 0, (int)file.Length);
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Update))
{
var entry = zip.CreateEntry("Transfer.mp4");
using (var destinationStream = entry.Open())
using (var sourceStream = File.OpenRead(sourceFile))
{
sourceStream.CopyTo(destinationStream);
}
}
}

My guess is that even though you've read the file and altered it, you didn't write anything back, i.e. changes persisted to MemoryStream, and didn't go anywhere after that.
You could try this (assumes you're using System.IO.Compression.ZipFile):
using (var zip = ZipFile.Open(destinationZip, ZipArchiveMode.Update))
{
var entry = zip.CreateEntry("Transfer.mp4");
using (var destinationStream = entry.Open())
using (var sourceStream = File.OpenRead(sourceFile))
{
sourceStream.CopyTo(destinationStream);
}
}
Or, if you're using ICSharpCode.SharpZipLib.Zip.ZipFile, do this:
using (var fileStream = new FileStream(destinationZip, FileMode.Open))
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Update))
{
var entry = zip.CreateEntry("Transfer.mp4");
using (var destinationStream = entry.Open())
using (var sourceStream = File.OpenRead(sourceFile))
{
sourceStream.CopyTo(destinationStream);
}
}

I'm still not sure why my initial code did not work, but I got it working by doing this instead:
string sourceFile = basePath + "Source_File.mp4";
string destinationZip = basePath + "Desintation_Zip.zip";
using (var destinationZipFileStream = new FileStream(destinationZip, FileMode.Open))
using (var destinationZipArchive = new ZipArchive(destinationZipFileStream, ZipArchiveMode.Update))
{
ZipArchiveEntry destinationWriter = destinationZipArchive.CreateEntry("Transfer.mp4");
using (var writer = new StreamWriter(destinationWriter.Open()))
using (var sourceStream = File.OpenRead(sourceFile))
{
sourceStream.CopyTo(writer.BaseStream);
}
}
Note: you will need to double check to make sure the file doesn't exist already or else you'll make duplicate files this way.

Related

zipping in-stream to a csv using ZipArchive, CsvHelper, and Azure Blobs leaves me with an empty archive C#

I am trying to upload the zipped the results of an sql query converted to a csv to a blob.
See my code below:
//this doesn't work
using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
using (var blobWriteStream = appendBlobClient.OpenWrite(true))
using (var zipArchive = new ZipArchive(blobWriteStream, ZipArchiveMode.Create))
using (var streamWriter = new StreamWriter(zipArchive.CreateEntry(fileName + ".csv").Open()))
using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.InvariantCulture)))
{
var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);
generator.FeedHeader(csvWriter);
{
while (sqlDataReader.Read())
{
generator.FeedRow(csvWriter, sqlDataReader);
}
}
streamWriter.Flush();
}
I end up with the following empty archive in my blob:
However if I don't bother with zipping, everything works out:
//this works
using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
using (var blobWriteStream = appendBlobClient.OpenWrite(true))
using (var streamWriter = new StreamWriter(blobWriteStream))
using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.InvariantCulture)))
{
var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);
generator.FeedHeader(csvWriter);
{
while (sqlDataReader.Read())
{
generator.FeedRow(csvWriter, sqlDataReader);
}
}
streamWriter.Flush();
}
Here's what the result looks like:
Am I using the ZipArchive / ZipEntry streams incorrectly? How can I fix it?
I believe you need to tell ZipArchive to keep the Stream open so that you can write to it with CsvHelper
using (var zipArchive = new ZipArchive(blobWriteStream, ZipArchiveMode.Create, true))
If that doesn't work, you may be able write to a MemoryStream first and then upload with your appendBlobClient. The first part works, I was able to write to a local file on my computer. I wasn't able to test the appendBlobClient part, since I'm not setup with an Azure account.
using (var memoryStream = new MemoryStream())
{
using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
using (var streamWriter = new StreamWriter(zipArchive.CreateEntry(fileName + ".csv").Open()))
using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.InvariantCulture)))
{
var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);
generator.FeedHeader(csvWriter);
{
while (sqlDataReader.Read())
{
generator.FeedRow(csvWriter, sqlDataReader);
}
}
streamWriter.Flush();
}
using (var blobWriteStream = appendBlobClient.OpenWrite(true))
{
memoryStream.Seek(0, SeekOrigin.Begin);
blobWriteStream.AppendBlock(memoryStream, memoryStream.Length);
}
}
The fix was to update my Azure.Storage.Blobs package to latest.
I was on version 12.6.0. Once I updated to 12.8.3, everything worked fine.
Here's how my call looks for anyone coming across this in the future:
var appendBlobClient = BlobStorage.CreateAppendBlobClient(blobName);
using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
using (var blobWriteStream = appendBlobClient.OpenWrite(true))
using (var zipArchive = new ZipArchive(blobWriteStream, ZipArchiveMode.Create))
using (var zipEntryStream = zipArchive.CreateEntry(fileName + ".csv").Open())
using (var streamWriter = new StreamWriter(zipEntryStream))
using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.CurrentCulture)))
{
// this class is a csvWriter helper
var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);
generator.FeedHeader(csvWriter); // this writes some comments at the top of the csv file
while (sqlDataReader.Read())
{
generator.FeedRow(csvWriter, sqlDataReader); // this writes records
}
}

C# ZipArchive - How to nest internal .zip files without writing to disk

I need to create a zip file in memory, then send the zip file to the client. However, there are cases where the created zip file will need to contain other zip files that were also generated in memory. For instance, the file structure might look like this:
SendToClient.zip
InnerZip1.zip
File1.xml
File2.xml
InnerZip2.zip
File3.xml
File4.xml
I've been attempting to use the System.IO.Compression.ZipArchive library. I cannot use the System.IO.Compression.ZipFile library because my project's version of .NET is not compatible with it.
Here's an example of what I've tried.
public Stream GetMemoryStream() {
var memoryStream = new MemoryStream();
string fileContents = "Lorem ipsum dolor sit amet";
string entryName = "Lorem.txt";
string innerZipName = "InnerZip.zip";
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
ZipArchiveEntry entry = archive.CreateEntry(Path.Combine(innerZipName, entryName), CompressionLevel.Optimal);
using (var writer = new StreamWriter(entry.Open())) {
writer.Write(fileContents);
}
}
return memoryStream
}
However, this just puts Lorem.txt in a folder called "Inner.zip" (instead of in an actual zip file).
I can create an empty inner zip file if I create an entry called "Inner.zip" without writing to it. I can't add anything to it, though, and writing to an entry called "Inner.zip\Lorem.txt" afterward just creates a folder again (alongside the identically named empty .zip file).
I've also tried creating a separate archive, serializing it with a memory stream, then writing that to the original archive as a .zip.
public Stream CreateOuterZip() {
var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
ZipArchiveEntry entry = archive.CreateEntry("Outer.zip", CompressionLevel.NoCompression);
using (var writer = new BinaryWriter(entry.Open())) {
writer.Write(GetMemoryStream().ToArray());
}
}
return memoryStream;
}
This just creates an invalid .zip file that windows doesn't know how to open, though.
Thanks in advance!
So I created a FileStream instead of a MemoryStream so the code can be tested easier
public static Stream CreateOuterZip()
{
string fileContents = "Lorem ipsum dolor sit amet";
// Final zip file
var fs = new FileStream(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SendToClient.zip"), FileMode.OpenOrCreate);
// Create inner zip 1
var innerZip1 = new MemoryStream();
using (var archive = new ZipArchive(innerZip1, ZipArchiveMode.Create, true))
{
var file1 = archive.CreateEntry("File1.xml");
using (var writer = new BinaryWriter(file1.Open()))
{
writer.Write(fileContents); // Change fileContents to real XML content
}
var file2 = archive.CreateEntry("File2.xml");
using (var writer = new BinaryWriter(file2.Open()))
{
writer.Write(fileContents); // Change fileContents to real XML content
}
}
// Create inner zip 2
var innerZip2 = new MemoryStream();
using (var archive = new ZipArchive(innerZip2, ZipArchiveMode.Create, true))
{
var file3 = archive.CreateEntry("File3.xml");
using (var writer = new BinaryWriter(file3.Open()))
{
writer.Write(fileContents); // Change fileContents to real XML content
}
var file4 = archive.CreateEntry("File4.xml");
using (var writer = new BinaryWriter(file4.Open()))
{
writer.Write(fileContents); // Change fileContents to real XML content
}
}
using (var archive = new ZipArchive(fs, ZipArchiveMode.Create, true))
{
// Create inner zip 1
var innerZipEntry = archive.CreateEntry("InnerZip1.zip");
innerZip1.Position = 0;
using (var s = innerZipEntry.Open())
{
innerZip1.WriteTo(s);
}
// Create inner zip 2
var innerZipEntry2 = archive.CreateEntry("InnerZip2.zip");
innerZip2.Position = 0;
using (var s = innerZipEntry2.Open())
{
innerZip2.WriteTo(s);
}
}
fs.Close();
return fs; // The file is written, can probably just close this
}
You can obviously modify this method to return a MemoryStream, or change the method to Void to just have the zip file written out to disk
You should create ZipArchive for internal zip file also. Write it to stream (memorystream). And after write this stream as general stream into main zip.
static Stream Inner() {
var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
var demoFile = archive.CreateEntry("foo2.txt");
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream)) {
streamWriter.Write("Bar2!");
}
}
return memoryStream;
}
static void Main(string[] args) {
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!");
}
var zip = archive.CreateEntry("inner.zip");
using (var entryStream = zip.Open()) {
var inner = Inner();
inner.Seek(0, SeekOrigin.Begin);
inner.CopyTo(entryStream);
}
}
using (var fileStream = new FileStream(#"d:\test.zip", FileMode.Create)) {
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
Thanks to this answer.

Re-create zip file from another zip file

Given a zip file, I need to re-create it with a specified compression level (eg, no compression).
I'm nearly there, but get the error:
Failed: Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory.
If I save the recreated zip file to windows, it looks like it's correct (correct file size, entries all exist with correct file sizes) but none of the files are extractable.
public static byte[] ReCompress(byte[] originalArchive, CompressionLevel newCompressionLevel)
{
var entries = new Dictionary<string, byte[]>();
///////////////////////////
// STEP 1: EXTRACT ALL FILES
///////////////////////////
using (var ms = new MemoryStream(originalArchive))
using (var originalZip = new ZipArchive(ms, ZipArchiveMode.Read))
{
foreach (var entry in originalZip.Entries)
{
var isFolder = entry.FullName.EndsWith("/");
if (!isFolder)
{
using (var stream = entry.Open())
using (var entryMS = new MemoryStream())
{
stream.CopyTo(entryMS);
entries.Add(entry.FullName, entryMS.ToArray());
}
}
else
{
entries.Add(entry.FullName, new byte[0]);
}
}
}
///////////////////////////
// STEP 2: BUILD ZIP FILE
///////////////////////////
using (var ms = new MemoryStream())
using (var newArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var uncompressedEntry in entries)
{
var newEntry = newArchive.CreateEntry(uncompressedEntry.Key, newCompressionLevel);
using (var entryStream = newEntry.Open())
using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
{
writer.Write(uncompressedEntry.Value);
}
}
return ms.ToArray();
}
}
At the end of the function if I do:
File.WriteAllBytes(#"D:\test.zip", ms.ToArray());
It creates a correctly structure archive sized 90mb but no files are extractable.
If I end with return ms.ToArray() it returns a ~130kb byte array.
Zip archive is broken because you read its content from MemoryStream before it is finished. In order to finish archive creation you need to call newArchive.Dispose() before calling ms.ToArray().
In this particular case you can do it like this:
using (var ms = new MemoryStream())
{
using (var newArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var uncompressedEntry in entries)
{
var newEntry = newArchive.CreateEntry(uncompressedEntry.Key, newCompressionLevel);
using (var entryStream = newEntry.Open())
using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
{
writer.Write(uncompressedEntry.Value);
}
}
}
return ms.ToArray();
}

Byte array getting corrupted when passed to another method

I have a bunch of Jpg images in byte array form. I want to add these to a zip file, turn the zip file into a byte array, and pass it to somewhere else. In a method, I have this code:
var response = //some response object that will hold a byte array
using (var ms = new MemoryStream())
{
using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var i = 1;
foreach (var image in images) // some collection that holds byte arrays.
{
var entry = zipArchive.CreateEntry(i + ".jpg");
using (var entryStream = entry.Open())
using (var compressStream = new MemoryStream(photo.ImageOriginal))
{
compressStream.CopyTo(entryStream);
}
i++;
}
response.ZipFile = ms.ToArray();
}
using (var fs = new FileStream(#"C:\Users\MyName\Desktop\image.zip", FileMode.Create))
{
ms.Position = 0;
ms.CopyTo(fs);
}
}
return response;
Now, I've added a filestream near the bottom to write it to a zipfile right away for testing purposes. This works, I get a zip file with 1 or more images in it on my desktop. However: response.ZipFile can not be made into a valid zipfile in the same way. I have tried this:
using (var ms2 = new MemoryStream(response.ZipFile))
using (var fs = new FileStream(#"C:\Users\Bara\Desktop\image.zip", FileMode.Create))
{
ms2.Position = 0;
ms2.CopyTo(fs);
}
But that creates a zipfile that can not be opened.
What I'm trying to do: turn response.ZipFileinto an array that can be turned into a working zipfile again. What am I doing wrong in this code?
How do you know that ZipArchive's Dispose doesn't write more to the underlying stream?
You should move this line to be after disposing the ZipArchive:
response.ZipFile = ms.ToArray();
Full code:
var response = //some response object that will hold a byte array
using (var ms = new MemoryStream())
{
using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var i = 1;
foreach (var image in images) // some collection that holds byte arrays.
{
var entry = zipArchive.CreateEntry(i + ".jpg");
using (var entryStream = entry.Open())
using (var compressStream = new MemoryStream(photo.ImageOriginal))
{
compressStream.CopyTo(entryStream);
}
i++;
}
}
response.ZipFile = ms.ToArray();
}
return response;

C# Create ZIP Archive with multiple files

I'm trying to create a ZIP archive with multiple text files as follows:
Dictionary<string, string> Values = new Dictionary<string, string>();
using (var memoryStream = new MemoryStream())
{
string zip = #"C:\Temp\ZipFile.zip";
foreach (var item in Values)
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var file = archive.CreateEntry(item.Key + ".txt");
using (var entryStream = file.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write(item.Value);
}
}
}
using (var fileStream = new FileStream(zip, FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
However, the ZIP is created with only the last text file, what's wrong?
You are creating ZipArchive on each iteration. Swapping foreach and using should solve it:
Dictionary<string, string> Values = new Dictionary<string, string>();
using (var memoryStream = new MemoryStream())
{
string zip = #"C:\Temp\ZipFile.zip";
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var item in Values)
{
var file = archive.CreateEntry(item.Key + ".txt");
using (var entryStream = file.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write(item.Value);
}
}
}
using (var fileStream = new FileStream(zip, FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
Each time your foreach loop runs it has the ZipArchiveMode as Create. That should be the problem, so it generates new zip everytime with new content on it, such as the last text file. Create an exception for each loop run after the first one it should be solved.

Categories