C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Zipping and Unzipping Files


Posted Date: 26 May 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: vijethaMember Level: Gold    
Rating: Points: 50



first ImportNamespaces

using System.IO;
using System.IO.Compression;

Add This Code For Zipping
-------------------------------

FileStream objFSIN=null;
FileStream objFsOp=null;
GZipStream objGZ=null;
byte[] btArr;
int intCount = 0;
try
{
objFsOp = new FileStream("C:\\Zip\\New\\Desktop\\New Folder\\AB.zip", FileMode.Create, FileAccess.Write, FileShare.None);
objGZ = new GZipStream(objFsOp,CompressionMode.Compress,true);
objFSIN = new FileStream("D:\\AB.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
btArr = new byte[objFSIN.Length];

intCount=objFSIN.Read(btArr,0,btArr.Length);
// objFSIN.Flush();
//objFSIN.Close();
objGZ.Write(btArr,0,btArr.Length);


}
catch
{

}
finally
{
if (objGZ != null)
{
objGZ.Flush();
objGZ.Close();
objGZ = null;

}
if (objFsOp != null)
{
objFsOp.Flush();
objFsOp.Close();

}
if (objFSIN != null)
{
objFSIN.Flush();
objFSIN.Close();


}
}


Add this code For Unzipping
-----------------------------------------------

FileStream fsin = null;
FileStream fsop = null;
GZipStream objgz=null;
const int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int count = 0;
try
{
fsin = new FileStream("C:\\Zip\\New\\Desktop\\New Folder\\AB.zip", FileMode.Open, FileAccess.Read, FileShare.Read);
fsop = new FileStream("C:\\Zip\\New\\Desktop\\New Folder\\AB.xml", FileMode.Create, FileAccess.Write, FileShare.None);
objgz = new GZipStream(fsin, CompressionMode.Decompress, true);
while (true)
{

count = objgz.Read(buffer, 0, bufferSize);

if (count != 0)
{

fsop.Write(buffer, 0, count);

}

if (count != bufferSize)
{

// have reached the end

break;

}

}


}
catch
{

}
finally
{
if (objgz != null)
{

objgz.Close();

objgz = null;

}

if (fsop != null)
{

fsop.Close();

fsop = null;

}

if (fsin != null)
{

fsin.Close();

fsin = null;

}
}




Responses

Author: Payal Jain    27 May 2008Member Level: Gold   Points : 2
throwing exception when i tried this code :(


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to select Multiple Select Calender Contol in ASP.NET and C#
Previous Resource: Updating Database through Dataset
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use