I am showing one example below where I have to update
my zip file in the WCF service and return back with modified zip file to end
user. Microsoft .NET 4.5 frame work, IO
compression library can be used to Zip and Unzip files.
Requirement
I have one zip file contains 5 or more images, I have to use
those images and generate one HTML file,
add into same zip file and return back zip file with one extra file.
Workflow to understand steps
C# Client Code
Namespace
using System.IO;
Constants
//Input
file contain 5 image file
const string inputFile = @"C:\TEMP\Test.zip";
//Output
file will contain 5 image and 1 Html file
const string outputFile = @"C:\TEMP\TestModified.zip";
Button event Code
private void button1_Click(object sender, EventArgs e)
{
//create
date object to count time taken in second
DateTime dt1 = DateTime.Now;
//Convert
file into byte array
byte[] docOutPut = File.ReadAllBytes(inputFile);
//create
WCF service instance
var client = new ServiceReference1.Service1Client();
//Call
WCF service method and get byte array
byte[] returnModifiedZipFile =
client.UpdateZipFile(docOutPut);
//convert
byte array into memory stream
Stream stream = new MemoryStream(returnModifiedZipFile);
//Create
new zip file and write memory stream into it
using (var fileStream = new FileStream(outputFile, FileMode.Create))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
lblHTML.Text = (DateTime.Now - dt1).Seconds
+ " Seconds";
}
WCF Service method code
Namespace
using System.IO;
using System.IO.Compression;
Add Reference to your
WCF project
System.IO.Compression.FileSystem
WCF Method Code
public byte[] UpdateZipFile(byte[] ZipFileInputFile)
{
//open
Memory Stream
using (var memoryStream = new MemoryStream())
{
//Write
byte array into memory stream to avoid “Memory Stream exception–Memory //stream
is not expandable” error
memoryStream.Write(ZipFileInputFile, 0,
ZipFileInputFile.Length);
//use
Zip Archive and open memory stream in update mpde
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Update, true))
{
//create
new file
var outPutFile = archive.CreateEntry("NewFile.html");
//open
new file to write
using (var entryStream = outPutFile.Open())
//create
stream writer and write content into new file
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("<html>");
streamWriter.Write("<body>");
//
read files how many files present into the zip file
for (int entry = 0; entry < archive.Entries.Count - 1; entry++)
{
//use
file name present into the zip file
streamWriter.Write(string.Format("<img src= \"{0}\"
width=\"100%\" />",
archive.Entries[entry].FullName));
streamWriter.Write("<p/>");
}
streamWriter.Write("</body>");
streamWriter.Write("</html>");
}
}
//convert
memory stream into array and retun
return memoryStream.ToArray();
}
}
To configure config file please see below article
https://www.blogger.com/blogger.g?blogID=7638493#allposts/postNum=0
References
To know more about Zip Archive and other IO Compression
utility see below