Hello , Can You Tell me How Can i Zip files Using C# , Please provide me the code and solution to this problem , also Can you Tell me that can i use Zip Files in C# Thanks in advance
Hello , Can You Tell me How Can i Zip files Using C# , Please provide me the code and solution to this problem , also Can you Tell me that can i use Zip Files in C# Thanks in advance
You Can Use these code For Using Zip Files in C#
private void zip(string[] args)
{
//Create an empty zip file
byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
FileStream fs = File.Create(args[1]);
fs.Write(emptyzip, 0, emptyzip.Length);
fs.Flush();
fs.Close();
fs = null;
//Copy a folder and its contents into the newly created zip file
Shell32.ShellClass sc = new Shell32.ShellClass();
// returns a Folder of args parameter
Shell32.Folder SrcFlder = sc.NameSpace(args[0]);
Shell32.Folder DestFlder = sc.NameSpace(args[1]);
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items, 0);
//Ziping a file using the Windows Shell API creates another thread where the zipping is executed.
//This means that it is possible that this console app would end before the zipping thread
//starts to execute which would cause the zip to never occur and you will end up with just
//an empty zip file. So wait a second and give the zipping thread time to get started
System.Threading.Thread.Sleep(1000);
}
and then u call this method
string[] str = new string[2];
str[0] = zipFolderPath; //zipFolderPath where you want to attachment zip file is added
str[1] = _startUpPath + "\\" + "Attachment.zip"; //path contain zip file
zip(str); //call zip method.
Hello , You Can Use thes Code
FileStream sourceFile = File.OpenRead(@"C:\sample.xml");
FileStream destFile = File.Create(@"C:\sample.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}
How to Zip/Unzip files using C#.NET
using System.IO.Compression;
using System.IO;
Example data to run ZipFile function:
sourceFile = @"D:\"
destinationFile = @"C:"
Private bool ZipFile(string sourceFile, string destinationFile)
{
using (FileStream oldFile = File.OpenRead(sourceFile)
using (FileStream newFile = File.Create(destinationFile)
using (GZipStream compression = new GZipStream(newFile, CompressionMode.Compress))
{
byte[] buffer = new byte[1024];
int numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
while (numberOfBytesRead > 0)
{
compression.Write(buffer, 0, numberOfBytesRead);
numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
}
compression.Close();
}
}
Example data to run UnZipFile function :
sourceFile = @"C:\backup\standards.zip
destinationFile = @"C:\backup\standards.pdf"
Private bool UnZipFile(string sourceFile, string destinationFile)
{
using(FileStream compressFile = File.Open(sourceFile,FileMode.Open))
using (FileStream uncompressedFile = File.Create(destinationFile)
using (GZipStream compression = new GZipStream(compressFile,
CompressionMode.Decompress))
{
int data = compression.ReadByte();
while(data != -1)
{
uncompressedFile.WriteByte((byte) data);
data = compression.ReadByte();
}
compression.Close();
}
}
Bookmarks