Old code C # decompress and compress the ZIP file to save it as a collection ~~

Source: Internet
Author: User

Note: This code is a little bug, so you have to use it together ~~ Bug1: If duplicate files exist, a window indicating whether to overwrite the files is displayed on the server. Therefore, do not duplicate the directories when extracting files. Bug2: it is the compression time. Similarly, there should be no duplicate names. In addition, at the beginning, create a zip file, copy it, and add it to it. The overall idea is that it can be used together. Haha ~~

 

The Code is as follows .. Copy it from the Internet, and change it by yourself ~~

 

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
{
Public String dosay = "Err ";
Protected void page_load (Object sender, eventargs E)
{
Dosay = server. mappath ("zzcn.net.zip ");


}
Protected void button#click (Object sender, eventargs E)
{
String err;
If (unzipfile (server. mappath ("zzcn.net.zip"), server. mappath ("zzcn.net"), Out ERR ))
{
Dosay = "decompressed successfully! ";
}
}

# Region decompress a zip file
/// <Summary>
/// Function: Decompress the ZIP file.
/// </Summary>
/// <Param name = "zipfilepath"> compressed file path </param>
/// <Param name = "unzipdir"> decompress the file storage path. If it is null, folders with the same name as compressed files are in the same directory as compressed files by default. </param>
/// <Param name = "Err"> error message </param>
/// <Returns> whether the decompression is successful </returns>
Public bool unzipfile (string zipfilepath, string unzipdir, out string ERR)
{
Err = "";
If (zipfilepath. Length = 0)
{
Err = "the compressed file cannot be blank! ";
Return false;
}
Else if (! Zipfilepath. endswith (". Zip "))
{
Err = "the file format is incorrect! ";
Return false;
}
Else if (! System. Io. file. exists (zipfilepath ))
{
Err = "the compressed file does not exist! ";
Return false;
}
// When the decompressed folder is empty, the folder with the same name as the compressed file is in the same level as the compressed file by default.
If (unzipdir. Length = 0)
Unzipdir = zipfilepath. Replace (system. Io. Path. getfilename (zipfilepath), system. Io. Path. getfilenamewithoutextension (zipfilepath ));
If (! Unzipdir. endswith ("\\"))
Unzipdir + = "\\";
If (! System. Io. Directory. exists (unzipdir ))
System. Io. Directory. createdirectory (unzipdir );
Try
{
Shell32.shellclass SC = new shell32.shellclass ();
Shell32.folder srcfolder = SC. namespace (zipfilepath );
Shell32.folder destfolder = SC. namespace (unzipdir );
Shell32.folderitems items = srcfolder. Items ();
Destfolder. copyhere (items, 20 );
}
Catch (exception ex)
{
Err = ex. message;
Return false;
}
Return true;
} // Decompress the package
# Endregion

Public bool buildframe (string srcfile, string destfolder)
{
Try
{
Shell32.shellclass SC = new shell32.shellclass ();
Shell32.folder srcfolder = SC. namespace (srcfile );
Shell32.folder destfolder = SC. namespace (destfolder );
Shell32.folderitems items = srcfolder. Items ();
Destfolder. copyhere (items, 20 );
Return true;
}
Catch
{
Return false;
}
}

Public bool Yasuo (string folder, string objfile)
{
Try
{
Shell32.shellclass SC = new shell32.shellclass ();
Shell32.folder srcfolder = SC. namespace (secure folder );
Shell32.folder destfolder = SC. namespace (objfile );
Shell32.folderitems items = srcfolder. Items ();
Destfolder. copyhere (items, 20 );
Return true;
}
Catch
{
Return false;
}
}

Protected void button2_click (Object sender, eventargs E)
{
If (Yasuo (server. mappath ("ZZ"), server. mappath ("zz1.zip ")))
{
Dosay = "compressed! Welcome to t.sina.com.cn/pengchenggang ";
}
}
}

Related Article

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.