Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-06-2009
Member
 
Join Date: Feb 2009
Posts: 26
How to Zip Files Using C#

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
Reply With Quote
  #2  
Old 05-06-2009
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to Zip Files Using C#

You Can Use these code For Using Zip Files in C#

Quote:
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.
Reply With Quote
  #3  
Old 05-06-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: How to Zip Files Using C#

Hello , You Can Use thes Code

Quote:
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();
}
Reply With Quote
  #4  
Old 05-06-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to Zip Files Using C#

How to Zip/Unzip files using C#.NET

Quote:
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();
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Zip Files Using C#"
Thread Thread Starter Forum Replies Last Post
Is it ok to delete setup log files and debug dump files in Disk Cleanup Ekram Operating Systems 7 04-05-2012 12:37 PM
The office temp files are not deleted after saving files in Excel 2003 Kanshin MS Office Support 2 23-02-2012 01:15 PM
Unable to add files while streaming files with Windows Media Player 11 to Xbox? Bhuvana21 Windows Software 6 12-07-2011 12:17 AM
importing outlook pst-files and Mozilla Thunderbird-files into Windows Mail IJAYA Windows Vista Mail 2 10-06-2010 08:21 PM
OSCheck : Detects Missing files, corrupted files and out of date system files Astalavista Windows Software 2 10-06-2009 11:16 AM


All times are GMT +5.5. The time now is 11:56 AM.