Hi,
Can anyone tell me if there is any inbuilt utility in windows shell API with the help of which we can Unzip the files. I need to use that and Unzip the files using c# code.
To my knowledge there isn't anything in the .net framework, but it's a great idea, trying to get at something in the windows framework. I havne't seen anyone do it, but I know there are a couple of open source routines using the zip/gzip libraries.
I'm using the nSoftware zip library, which is extremely easy to use. Literally a few ( <10 ) lines of code to write top open, create, add, or modify zip files. Password protectio and everything is there.
Not sure of your situation, but This is a case where I'd lean towards something that is already proven, and not re-invent the wheel.
Following is an example of decompressing a file having bytes in memory stream using sharpziplib.
ZipInputStream compressedBytes = new ZipInputStream(ms);
ZipEntry entry = compressedBytes.GetNextEntry();
byte[] uncompressedBytes =
new byte[entry.Size];
byte[] tempBuffer =
new byte[2048];
MemoryStream tempMemoryStream =
new MemoryStream();
int bytesRead = 0;
if (uncompressedBytes.Length > 0)
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
No, you cannot use those classes for processing ZIP files, at least not easily. System.IO.Compression does DeflateStream and GZipStream, which will neither read nor write a ZIP file. System.IO.Packaging will do ZIP files, but it is unwieldy for that purpose.
A complex interface and probably not what you want. This gap is why there are 3rd party libraries to do ZIP files in .NET.
SharpZipLib has been around a long time, but the interface is sort of clunky and error prone. To give you an example of what I mean, here is the "Extract into a memory stream" code for DotNetZip, that is equivalent to the post from above:
MemoryStream ms = new MemoryStream();
using (ZipFile zip = ZipFile.Read(ZipFileToRead))
{
zip["DataFile.csv"].Extract(ms);
}
That assumes ZipFileToRead is the name of a zipfile on disk that you can read. If you have the ZipFile data in a MemoryStream, and you want to extract an entry into a different MemoryStream, you can do that, too. Just replace ZipFileToRead in the above
with a readable Stream. Easy.
It's also easy to create a zip with DotNetZip and save it to Response.OutputStream, or to any disk file in a writable directory.
System.IO.Packaging will do ZIP files, but it is unwieldy for that purpose.
Yes, once again you're right, sadly enough. Unwieldy indeed.
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
Yes, the J# runtime can be used to process zip files.
I had a few problems with it, myself: First, the API. It's a complex API for handling basic zip functions. Look at all the code you have to write to zip and unzip. Also the feel of the API is very un-.NET - no generics, no .NET enumeration, no indexers,
no properties, no progress events, etc. All the fundamental .NET stuff is missing from the J# library, because it is based in Java and oriented to that language (c.1998, not the recent versions). This means that J# is not very friendly when being integrated
into other frameworks, like LINQ, ASP.NET (eg Data bound controls) or even Windows Forms.
Second, it drags in the J# runtime, which is huge, just huge. You can't just get the zip stuff, you have to take everything. And also, now J# is reaching end of support.
http://msdn.microsoft.com/en-us/vjsharp/default.aspx . It looks like mainstream support for the runtime will end in 2010, not long off now.
Third, the features of the java.util.zip library are limited and there is no chance of expansion for .NET languages. Poor encryption; no support for zip64, self-extracting archives, or Unicode; and so on.
Given all that, third-party libraries like DotNetZip and SharpZipLib evolved to fill the gap.
JKhera
Member
15 Points
3 Posts
Unzip files using c# code
Nov 17, 2005 03:54 AM|LINK
Hi,
Can anyone tell me if there is any inbuilt utility in windows shell API with the help of which we can Unzip the files. I need to use that and Unzip the files using c# code.
Regards
Jyoti
ScottCate
Member
745 Points
75 Posts
ASPInsiders
MVP
Re: Unzip files using c# code
Nov 17, 2005 02:07 PM|LINK
To my knowledge there isn't anything in the .net framework, but it's a great idea, trying to get at something in the windows framework. I havne't seen anyone do it, but I know there are a couple of open source routines using the zip/gzip libraries.
I'm using the nSoftware zip library, which is extremely easy to use. Literally a few ( <10 ) lines of code to write top open, create, add, or modify zip files. Password protectio and everything is there.
Not sure of your situation, but This is a case where I'd lean towards something that is already proven, and not re-invent the wheel.
http://www.nsoftware.com/ipworks/zip/
Scott Cate - myKB.com
Knowledge Base / FAQ Support Manager
Knowledge Base Software
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: Unzip files using c# code
Nov 17, 2005 08:19 PM|LINK
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
rmprimo
Contributor
3639 Points
738 Posts
Re: Unzip files using c# code
Nov 18, 2005 03:45 PM|LINK
Rob
izharulislam
Participant
1498 Points
296 Posts
Re: Unzip files using c# code
Jan 31, 2009 10:46 AM|LINK
Hi,
Following is an example of decompressing a file having bytes in memory stream using sharpziplib.
ZipInputStream compressedBytes = new ZipInputStream(ms);ZipEntry entry = compressedBytes.GetNextEntry();
byte[] uncompressedBytes = new byte[entry.Size]; byte[] tempBuffer = new byte[2048]; MemoryStream tempMemoryStream = new MemoryStream(); int bytesRead = 0; if (uncompressedBytes.Length > 0){
do{
bytesRead = compressedBytes.Read(tempBuffer, 0, tempBuffer.Length);
tempMemoryStream.Write(tempBuffer, 0, bytesRead);
} while (bytesRead > 0);}
compressedBytes.Close();
ms.Close();
Thanks
Izhar Ul Islam Khan
Microsoft Certified Technology Specialist
Svante
All-Star
18347 Points
2300 Posts
Re: Unzip files using c# code
Jan 31, 2009 11:53 AM|LINK
Do you *really* mean that? Why do you need it to be in the windows shell API?
If so: http://msdn.microsoft.com/en-us/library/system.io.compression.aspx and http://msdn.microsoft.com/en-us/library/system.io.packaging.aspx .
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
cheeso
Member
342 Points
75 Posts
Re: Unzip files using c# code
Feb 12, 2009 10:59 PM|LINK
No, you cannot use those classes for processing ZIP files, at least not easily. System.IO.Compression does DeflateStream and GZipStream, which will neither read nor write a ZIP file. System.IO.Packaging will do ZIP files, but it is unwieldy for that purpose. A complex interface and probably not what you want. This gap is why there are 3rd party libraries to do ZIP files in .NET.
SharpZipLib has been around a long time, but the interface is sort of clunky and error prone. To give you an example of what I mean, here is the "Extract into a memory stream" code for DotNetZip, that is equivalent to the post from above:
MemoryStream ms = new MemoryStream(); using (ZipFile zip = ZipFile.Read(ZipFileToRead)) { zip["DataFile.csv"].Extract(ms); }That assumes ZipFileToRead is the name of a zipfile on disk that you can read. If you have the ZipFile data in a MemoryStream, and you want to extract an entry into a different MemoryStream, you can do that, too. Just replace ZipFileToRead in the above with a readable Stream. Easy.
It's also easy to create a zip with DotNetZip and save it to Response.OutputStream, or to any disk file in a writable directory.
DotNetZip is available at http://www.codeplex.com/DotNetZip
ZIP on Server ZIP file ZIP
Svante
All-Star
18347 Points
2300 Posts
Re: Unzip files using c# code
Feb 13, 2009 07:19 AM|LINK
Sadly, you're quite right. Not easily, more code is required.
There is yet another option though, and that's to use the J# library implementation: http://msdn.microsoft.com/en-us/magazine/cc164129.aspx . I have not done so personally, but it does seem a viable alternative.
Yes, once again you're right, sadly enough. Unwieldy indeed.
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
suthish nair
All-Star
15176 Points
3304 Posts
Re: Unzip files using c# code
Feb 13, 2009 07:36 AM|LINK
using java.util;
using java.util.zip;
using java.io;
public string ZipFile(string savePath, string[] attachFiles)
{
string sFlag = "Error";
try
{
FileOutputStream fileOutput = new FileOutputStream(savePath);
ZipOutputStream zipOutput = new ZipOutputStream(fileOutput);
FileInputStream fileInput = null;
foreach (string file in attachFiles)
{
fileInput = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));
zipOutput.putNextEntry(zipEntry);
sbyte[] buffer = new sbyte[1024];
int len = 0;
while ((len = fileInput.read(buffer)) >= 0)
{
zipOutput.write(buffer, 0, len);
}
}
zipOutput.closeEntry();
fileInput.close();
zipOutput.close();
fileOutput.close();
return sFlag = "Success";
}
catch (Exception)
{
return sFlag;
}
}
private List<ZipEntry> GetZipFiles(ZipFile zipfill)
{
List listZip = new List();
Enumeration zipEnum = zipfill.entries();
while (zipEnum.hasMoreElements())
{
ZipEntry zipEntry = (ZipEntry)zipEnum.nextElement();
listZip.Add(zipEntry);
}
return listZip;
}
public string UnZipFile(string zippedFile, string destPath)
{
string sFlag = "Error";
try
{
ZipFile zipfile = new ZipFile(zippedFile);
List zipFiles = GetZipFiles(zipfile);
foreach (ZipEntry zipFile in zipFiles)
{
if (!zipFile.isDirectory())
{
InputStream s = zipfile.getInputStream(zipFile);
try
{
Directory.CreateDirectory(destPath + "\\" + Path.GetDirectoryName(zipFile.getName()));
FileOutputStream dest = new FileOutputStream(Path.Combine(destPath + "\\" + Path.GetDirectoryName(zipFile.getName()), Path.GetFileName(zipFile.getName())));
try
{
int len = 0;
sbyte[] buffer = new sbyte[7168];
while ((len = s.read(buffer)) >= 0)
{
dest.write(buffer, 0, len);
}
}
finally
{
dest.close();
}
}
finally
{
s.close();
}
}
sFlag = "Success";
}
return sFlag;
}
catch (Exception)
{
return sFlag;
}
}
cheeso
Member
342 Points
75 Posts
Re: Unzip files using c# code
Feb 13, 2009 01:43 PM|LINK
Yes, the J# runtime can be used to process zip files.
I had a few problems with it, myself: First, the API. It's a complex API for handling basic zip functions. Look at all the code you have to write to zip and unzip. Also the feel of the API is very un-.NET - no generics, no .NET enumeration, no indexers, no properties, no progress events, etc. All the fundamental .NET stuff is missing from the J# library, because it is based in Java and oriented to that language (c.1998, not the recent versions). This means that J# is not very friendly when being integrated into other frameworks, like LINQ, ASP.NET (eg Data bound controls) or even Windows Forms.
Second, it drags in the J# runtime, which is huge, just huge. You can't just get the zip stuff, you have to take everything. And also, now J# is reaching end of support. http://msdn.microsoft.com/en-us/vjsharp/default.aspx . It looks like mainstream support for the runtime will end in 2010, not long off now.
Third, the features of the java.util.zip library are limited and there is no chance of expansion for .NET languages. Poor encryption; no support for zip64, self-extracting archives, or Unicode; and so on.
Given all that, third-party libraries like DotNetZip and SharpZipLib evolved to fill the gap.