WPF用C#压缩文件,并且上传到服务器

我们的目的

我们是要将本地的一个文件夹里面的内容上传到服务器,但是服务器(自己搭的)那边不支持文件夹传送,故先要将文件夹压缩,再上传。项目要求越简便越好,所以压缩和上传放在一个button里面做了。

搭一个简单的测试界面

一个上传按钮,一个显示信息的label
WPF用C#压缩文件,并且上传到服务器_第1张图片之后就要实现压缩,以及上传的功能了

压缩

下载ICSharpCode.SharpZipLib.dll文件,放到你项目的debug(或者bin)目录下,在项目中添加引用dll文件,然后新建一个类ZipHelper.cs, 下面是类中的内容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Core;

namespace ZipOneCode.ZipProvider
{
    public class ZipHelper
    {
        /// 
        /// 压缩文件
        /// 
        /// 
        /// 
        public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
        {
            if (sourceFilePath[sourceFilePath.Length - 1] != Path.DirectorySeparatorChar)
            {
                sourceFilePath += Path.DirectorySeparatorChar;
            }
                
            ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
            zipStream.SetLevel(6);  // 压缩级别 0-9
            CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);

            zipStream.Finish();
            zipStream.Close();
        }

        /// 
        /// 递归压缩文件
        /// 
        /// 待压缩的文件或文件夹路径
        /// 打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名
        /// 
        private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
        {
            Crc32 crc = new Crc32();
            string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
            foreach (string file in filesArray)
            {
                if (Directory.Exists(file))                     //如果当前是文件夹,递归
                {
                    CreateZipFiles(file, zipStream, staticFile);
                }

                else                                            //如果是文件,开始压缩
                {
                    FileStream fileStream = File.OpenRead(file);

                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                    ZipEntry entry = new ZipEntry(tempFile);

                    entry.DateTime = DateTime.Now;
                    entry.Size = fileStream.Length;
                    fileStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zipStream.PutNextEntry(entry);

                    zipStream.Write(buffer, 0, buffer.Length);
                }
            }
        }


    }
}

上传文件事件函数

代码中的****,是你目标服务器的网址

		public void Upload_File(string file)
        {
            FileInfo info = new FileInfo(file);
            string url = "***************************";
            WebClient client = new WebClient();
            client.Credentials = CredentialCache.DefaultNetworkCredentials;
            client.UploadFileAsync(new Uri(url), file);
            //client.UploadFile(new Uri(url), "POST", file);
            client.UploadFileCompleted += new UploadFileCompletedEventHandler(result_UploadFileResult);
        }


        private void result_UploadFileResult(object sender, UploadFileCompletedEventArgs e)
        {
            if(e.Error != null)
            {
                Message.Content = "上传失败     " + e.Error.Message;
            }
            else
            {
                Message.Content = "上传成功";
            }
        }

之后添加button事件

		private void upload_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog P_File_Folder = new FolderBrowserDialog();

            if (P_File_Folder.ShowDialog() == form.DialogResult.OK)

            {
                WebClient myWebClient = new WebClient();
                //System.Windows.MessageBox.Show(P_File_Folder.SelectedPath);//选定目录后打印路径信息
                string str = P_File_Folder.SelectedPath + ".zip";
                ZipHelper.CreateZip(@P_File_Folder.SelectedPath, @str);

                Upload_File(str);
            }
        }

之后就可以测试了
WPF用C#压缩文件,并且上传到服务器_第2张图片

WPF用C#压缩文件,并且上传到服务器_第3张图片
WPF用C#压缩文件,并且上传到服务器_第4张图片

你可能感兴趣的:(C#,文件夹压缩与上传)