I want to compress a directory(folder) that includes multi files to (.zip) file in Asp.net 2 that runs in IIS 6 and i can not upgrade my .net version because i forced to run my project in IIS 6.
after i searched in google, i faced with below code. unfortunately this code is wrong and result of this code has wrong format.
public void CompressFile(string sDir, string sRelativePath, GZipStream zipStream)
{
//Compress file name
char[] chars = sRelativePath.ToCharArray();
zipStream.Write(BitConverter.GetBytes(chars.Length), 0, sizeof(int));
foreach (char c in chars)
zipStream.Write(BitConverter.GetBytes(c), 0, sizeof(char));
//Compress file content
byte[] bytes = File.ReadAllBytes(Path.Combine(sDir, sRelativePath));
zipStream.Write(BitConverter.GetBytes(bytes.Length), 0, sizeof(int));
zipStream.Write(bytes, 0, bytes.Length);
}
public void CompressDirectory(string sInDir, string sOutFile)
{
string[] sFiles = Directory.GetFiles(sInDir, "*.*", SearchOption.AllDirectories);
int iDirLen = sInDir[sInDir.Length - 1] == Path.DirectorySeparatorChar ? sInDir.Length : sInDir.Length + 1;
using (FileStream outFile = new FileStream(sOutFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (GZipStream str = new GZipStream(outFile, CompressionMode.Compress))
foreach (string sFilePath in sFiles)
{
string sRelativePath = sFilePath.Substring(iDirLen);
CompressFile(sInDir, sRelativePath, str);
}
}
the result of above code is a zip file that contains a file with unknown format.
Please reply to me how can i zip(compress) a directory in Asp.net 2 (IIS 6)?