How to use C # to compress files and note the problem!

Source: Internet
Author: User
Tags crc32 datetime zip
Problem | Compression preferred, first find an open source C # compression component.
such as: Icsharpcode.sharpziplib Download Address: http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
According to its help you can do what you need.
I'm using this component line and I'm having a problem.
There is nothing wrong with compressing small files, and once the source file reaches 150M, it can overwhelm your machine. (At least my machine)
Why, because if the source file is 150M, you need to request a byte array of 150M size in memory. A good machine is not a problem, but the general machine is miserable. If the file is big, the good machine can't stand it.
In order to solve the problem of large file compression, the method of segmented compression can be used.

private string Createzipfile (String Path,int M)
{
Try
{
Crc32 CRC = New Crc32 ();
ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipout=new ICSharpCode.SharpZipLib.Zip.ZipOutputStream ( System.IO.File.Create (path+ ". zip"));
System.IO.FileStream fs=system.io.file.openread (path);
Long pai=1024*1024*m;//write once per M trillion
Long Forint=fs. length/pai+1;
Byte[] Buffer=null;
ZipEntry entry = new ZipEntry (System.IO.Path.GetFileName (Path));
Entry. Size = fs. Length;
Entry. DateTime = DateTime.Now;
ZipOut. Putnextentry (entry);
for (long i=1;i<=forint;i++)
{
if (Pai*i<fs. Length)
{
Buffer = new Byte[pai];
Fs. Seek (pai* (i-1), System.IO.SeekOrigin.Begin);
}
Else
{
if (fs. Length<pai)
{
Buffer = new Byte[fs. Length];
}
Else
{
Buffer = new Byte[fs. Length-pai* (i-1)];
Fs. Seek (pai* (i-1), System.IO.SeekOrigin.Begin);
}
}
Fs. Read (Buffer,0,buffer. Length);
Crc. Reset ();
Crc. Update (buffer);
ZipOut. Write (buffer,0, buffer. Length);
ZipOut. Flush ();
}
Fs. Close ();
ZipOut. Finish ();
ZipOut. Close ();
System.IO.File.Delete (path);
Return path+ ". zip";
}
catch (Exception ex)
{
String Str=ex. message;
return path;
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.