Step 1: Save the last file to the server directory
/// <Summary> /// upload the compressed file /// </Summary> protected void uploadzip () {// defines the temporary directory for uploading files. String uploaddic = @ "C: \ test \ "; // determine whether to select the file if (fileupload1.filename. length> 0) {// determine the file format if (fileupload1.filename. endswith (". zip ") {// determine whether the Upload File directory exists if (! Directory. exists (uploaddic) {// This directory is created if it does not exist. createdirectory (uploaddic);} // store the compressed file fileupload1.saveas (uploaddic + fileupload1.filename) on the server; unpackzip (uploaddic + fileupload1.filename, uploaddic + fileupload1.filename. substring (0, fileupload1.filename. length-4) + @"\");}}}
Step 2: Decompress the file and create a temporary file directory. You must reference the external component icsharpcode. sharpziplib. dll.
/// <Summary> /// decompress the file /// </Summary> /// <Param name = "zipdir"> compress the file directory </param> /// <param name = "filedir"> extract the directory </param> // <returns> </returns> protected void unpackzip (string zipdir, string filedir) {try {// create a directory for extracting files if (! Directory. exists (filedir) {directory. createdirectory (filedir);} // create the zip output stream zipinputstream zipstream = new zipinputstream (file. openread (zipdir); zipentry theentry; // open the compressed file step by step while (theentry = zipstream. getnextentry ())! = NULL) {// obtain the name of the opened directory string dirname = path. getdirectoryname (theentry. name); // get the file name string filename = path. getfilename (theentry. name); If (dirname! = String. Empty) {// create a new directory named directory. createdirectory (filedir + dirname);} If (filename! = String. empty) {// Write File Content filestream filesteam = file. create (filedir + theentry. name); int size = 2048; byte [] DATA = new byte [size]; while (true) {size = zipstream. read (data, 0, Data. length); If (size> 0) {filesteam. write (data, 0, size) ;}else {break ;}} filesteam. close () ;}} zipstream. close (); // read the decompressed file readdirectory (filedir); // Delete the decompressed directory deletetempdirectory (filedir);} catch (exception E) {Throw new ioexception ("File Content exception" + E. message );}}
Step 3: Read the content of the temporary file
/// <Summary> /// read the extracted file content /// </Summary> /// <Param name = "filedir"> extract the directory </param> protected void readdirectory (string filedir) {// obtain all files in the decompressed file string [] files = directory. getfiles (filedir); If (files. count ()> 0) {// obtain the file foreach cyclically (string file in files) {// if it is XML, // xmldocument Doc = new xmldocument (); // Doc. load (File); // obtain a single file directory. getfiles (File); // read the file for data processing ...}}}
Step 4: Delete the temporary directory
/// <Summary> /// Delete the temporarily decompressed directory /// </Summary> /// <Param name = "filedir"> </param> protected void deletetempdirectory (string filedir) {If (directory. exists (filedir) {// Delete this directory and all file directories under this directory. delete (filedir, true );}}
Read ZIP file content