Use 7z to compress files in different volumes

Source: Internet
Author: User

Recently, I made an email sending tool, which has an attachment function,

If the attachment is too large, it is usually compressed by winara sub-volumes and then uploaded.

How can I use a program to implement automatic volume splitting and compression? Because RAR is not free, you can find 7z online.

 

Install 7z, copy 7z. DLL to the program directory, and reference the DLL sevenzipsharp.

Introduce using sevenzip. SDK. compression;
Using sevenzip; the two namespaces

 

Code

/// <Summary>
/// Cut the file. When the file size is greater than the capacity of the configuration file, the file is cut.
/// </Summary>
/// <Param name = "filepath"> path of the PDF file under user: EG user/2000/01/01/xxxx.pdf </param>
/// <Param name = "descdecpath"> User, 2000/01, 01, and temp </param>
/// <Returns> </returns>
Public bool splitfile (string filepath, string descdecpath)
{
Fileinfo finfo = new fileinfo (filepath );
String copyfilepath = path. Combine (descdecpath, finfo. Name );
// If (finfo. Length <= This. filesize) return false;

If (! Directory. exists (descdecpath ))
{
Directory. createdirectory (descdecpath );
}
File. Copy (filepath, copyfilepath );
Application. doevents ();

Sevenzipcompressor TMP = new sevenzipcompressor ();
Sevenzipcompressor. setlibrarypath (application. startuppath + "/7z. dll ");
// TMP. volumesize = finfo. Length <= This. filesize? 0: This. filesize;
TMP. volumesize = This. filesize; // volume size. up to 1000 files can be separated.
TMP. archiveformat = outarchiveformat. sevenzip;
TMP. compressdirectory (descdecpath, copyfilepath );

File. Delete (copyfilepath); // Delete the copy object

If (directory. getfiles (descdecpath). Length = 1)
{
Foreach (string path in directory. getfiles (descdecpath ))
{
File. Delete (PATH );
}
TMP. volumesize = 0;
TMP. compressfiles (copyfilepath + ". 7z ",
New String [] {filepath}); // compressed file

}
Application. doevents ();
Return true;

}

 

Because there is no file sharding compression class library, I would like to perform the second step: Use the folder sharding compression, copy the file to a temporary folder, and then compress the Temporary Folder.

See the Code:

 

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.