C on zip compression and decompression help class package with the source download
- 2020-05-09 19:11:08
- OfStack
c# under compression decompression, mainly with the third square library for encapsulation. ICSharpCode.SharpZipLib.dll class library, link to your official download link. Compression is compressed primarily in the form of a stream.
Let me conclude. As a high-level language, C# has a powerful class library and a third party library. You can do a lot of things. But also has the disadvantage, USES the third square class library the performance is not very high. I compress hundreds of M things. cpu went over 50% in an instant. Far worse than 360 compression and zip compression performance. So this class also applies to things with less compression.
Compress files and folders. File compression is as simple as streaming the files to be compressed into memory and into the compression stream. That's it. Folders are a little bit of a hassle. Because you want to unzip the folder to keep the folder file hierarchy. So my way of doing this is to recursively iterate through the files in the folder. Calculate its relative position in the compressed flow.
The following code
/// <summary>
/// Compress files or folders
/// </summary>
/// <param name="_depositPath"> The location of the compressed file Such as C:\\windows\abc.zip</param>
/// <returns></returns>
public bool CompressionZip(string _depositPath)
{
bool result = true;
FileStream fs = null;
try
{
ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));
ComStream.SetLevel(9); // Compression level
foreach (string path in AbsolutePaths)
{
// If it's a directory
if (Directory.Exists(path))
{
ZipFloder(path, ComStream, path);
}
else if (File.Exists(path))// If it's a file
{
fs = File.OpenRead(path);
byte[] bts = new byte[fs.Length];
fs.Read(bts, 0, bts.Length);
ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
ComStream.PutNextEntry(ze); // Provides for compressed file streams 1 A container
ComStream.Write(bts, 0, bts.Length); // Write a byte
}
}
ComStream.Finish(); // The end of the compression
ComStream.Close();
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
errorMsg = ex.Message;
result = false;
}
return result;
}
// Compressed folder
private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
{
foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
{
if (Directory.Exists(item.FullName))
{
ZipFloder(_OfloderPath, zos, item.FullName);
}
else if (File.Exists(item.FullName))// If it's a file
{
DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
string fullName2 = new FileInfo(item.FullName).FullName;
string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);// Get relative directory
FileStream fs = File.OpenRead(fullName2);
byte[] bts = new byte[fs.Length];
fs.Read(bts, 0, bts.Length);
ZipEntry ze = new ZipEntry(path);
zos.PutNextEntry(ze); // Provides for compressed file streams 1 A container
zos.Write(bts, 0, bts.Length); // Write a byte
}
}
}
The decompression is much easier. There are files unzip files, there are folders to traverse, unzip files. The extracted file already contains its hierarchical relationship to the folder.
/// <summary>
/// Unpack the
/// </summary>
/// <param name="_depositPath"> Compressed file path </param>
/// <param name="_floderPath"> Unzip path </param>
/// <returns></returns>
public bool DeCompressionZip(string _depositPath, string _floderPath)
{
bool result = true;
FileStream fs=null;
try
{
ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));
ZipEntry ze = InpStream.GetNextEntry();// Gets each in the compressed file 1 A file
Directory.CreateDirectory(_floderPath);// Create the unzip folder
while (ze != null)// If you're done ze It is null
{
if (ze.IsFile)// The compression zipINputStream It's full of files. The name of the file with the folder is the folder \\ The file name
{
string[] strs=ze.Name.Split('\\');// If included in the file name '\\ 'indicates a folder
if (strs.Length > 1)
{
// The two-layer loop is used 1 layer 1 Layer create folder
for (int i = 0; i < strs.Length-1; i++)
{
string floderPath=_floderPath;
for (int j = 0; j < i; j++)
{
floderPath = floderPath + "\\" + strs[j];
}
floderPath=floderPath+"\\"+strs[i];
Directory.CreateDirectory(floderPath);
}
}
fs = new FileStream(_floderPath+"\\"+ze.Name, FileMode.OpenOrCreate, FileAccess.Write);// Create a file
// Loop reads the file into the file stream
while (true)
{
byte[] bts = new byte[1024];
int i= InpStream.Read(bts, 0, bts.Length);
if (i > 0)
{
fs.Write(bts, 0, i);
}
else
{
fs.Flush();
fs.Close();
break;
}
}
}
ze = InpStream.GetNextEntry();
}
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
errorMsg = ex.Message;
result = false;
}
return result;
}
Let me conclude. As a high-level language, C# has a powerful class library and a third party library. You can do a lot of things. But also has the disadvantage, USES the third square class library the performance is not very high. I compress hundreds of M things. cpu went over 50% in an instant. Far worse than 360 compression and zip compression performance. So this class also applies to things with less compression.
Full example download address