Query regarding compressing Folder in ASP.net  
Author Message
Triiiiiiiii





PostPosted: Visual C# General, Query regarding compressing Folder in ASP.net Top

Hi All,

Can anybody tell me the way by which I can compress a folder in Asp.net. I've searched for some options on internet and have got one solution for compressing file using GZipStream.

Below is the code for the same:

//A String object reads the file name (locally)

string fileName = Path.GetFileName(browseFile.Value);

//Stream object that reads file contents

Stream streamObj = browseFile.PostedFile.InputStream;

//Allocate space in buffer according to the length of the file read

byte[] buffer = new byte[streamObj.Length];

//Fill buffer

streamObj.Read(buffer, 0, buffer.Length);

streamObj.Close();

//File Stream object used to change the extension of a file

System.IO.FileStream compFile = File.Create(MapPath(fileName + ".gzip"));

//GZip object that compress the file

GZipStream zipStreamObj = new GZipStream(compFile, CompressionMode.Compress);

//Write to the Stream object from the buffer

zipStreamObj.Write(buffer, 0, buffer.Length);

zipStreamObj.Close();

Can anybody tell me that how can I zip a folder using ASP.net.

I would really appreciate if some one has solution for this.

Thanks in advance,

Tripti



Visual C#14  
 
 
TomPearson





PostPosted: Visual C# General, Query regarding compressing Folder in ASP.net Top

I have used the winzip command line add on to do this in the past.
Simply run the command line object from the code by creating a process object for the command line exe, passing the required arguments including the path of the file to zip and the output path.
You have to pay for the command line add on if using it comercially.

Alternatively there are several zip class available for download - try googling ."NET zip" These are mostly free.


 
 
Triiiiiiiii





PostPosted: Visual C# General, Query regarding compressing Folder in ASP.net Top

Hi Tom,

Thanks for your reply. Well, I've found the solution of my problem. I've used Shell32 class for the same.Below is the solution:

string originalFilesPath;

protected void Page_Load(object sender, EventArgs e)

{

bool isCreated;

bool isAdded;

string zipFileName;

if (!Page.IsPostBack)

{

originalFilesPath = "c:\\tripti\\";

zipFileName = "c:\\test.zip";

isCreated = CreateZip(zipFileName);

if (isCreated == true)

{

isAdded = AddZip("Timesheet", zipFileName);

lblStatus.Text = zipFileName + " file has been created succesfully";

}

}

}

public bool CreateZip(string ZipFileName)

{

try

{

//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(ZipFileName);

fs.Write(emptyzip, 0, emptyzip.Length);

fs.Flush();

fs.Close();

fs = null;

}

catch (Exception ex)

{

return false;

}

return true;

}

 

 

public bool AddZip(string FileName, string ZipFileName)

{

try

{

Shell32.ShellClass sc = new Shell32.ShellClass();

Shell32.Folder SrcFlder = sc.NameSpace(originalFilesPath);

Shell32.FolderItem fi = SrcFlder.Items().Item(FileName);

Shell32.Folder DestFlder = sc.NameSpace(ZipFileName);

DestFlder.CopyHere(fi, 20);

Thread.Sleep(500);

}

catch (Exception ignore)

{

return false;

}

return true;

}

Tripti