zoukankan      html  css  js  c++  java
  • 多媒体照片打成压缩包上传 (二)

    using ICSharpCode.SharpZipLib.Zip;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Media
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                FileUploadDTO dt = new FileUploadDTO();
                DateTime startTime;
                TimeSpan resultTime;
                ulong dateVal = 0;
                OpenFileDialog f1 = new OpenFileDialog();
                f1.Title = "文件上传";
                f1.Filter = "压缩包(*.zip)|*.zip";
                string deletepath = string.Empty;
                // string mediapath = string.Empty; 
                if (f1.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fileInfo = new FileInfo(f1.FileName);
                  //  mediapath = f1.FileName; //压缩包服务器上路径
                    int K = 0;
                    if (!string.IsNullOrEmpty(f1.FileName))
                    {
                        startTime = System.DateTime.Now;
                        //传入压缩文件路径在服务器上进行解压,服务器上生成一个名为测试的文件夹
                        UnpackFiles(f1.FileName, Path.GetDirectoryName(f1.FileName) + "\测试\");
                        string path = Path.GetDirectoryName(f1.FileName) + "\测试\" + Path.GetFileNameWithoutExtension(f1.FileName);//解压后文件路径
                        deletepath = Path.GetDirectoryName(f1.FileName) + "\测试";//测试文件夹路径
                        //循环遍历解压后的文件,获取里面的数据
                        List<string> list = GetAllFiles(path);
                        foreach (var i in list)
                        {
                            dt.name = Path.GetFileName(i);//9527.txt
                            dt.ext = Path.GetExtension(i);
                            Image image = Image.FromFile( Path.GetFullPath(i));
                            dt.Data = ImageToBytes(image);
                            dt.Offset = 0;
                            Mediaupload _fi = new Mediaupload();
                            _fi.segmentReadUpload(dt);
                            K++;
                        }
                        resultTime = System.DateTime.Now - startTime;
                        dateVal = (ulong)resultTime.TotalSeconds;
                        if (MessageBox.Show(string.Format("导入成功!共导入{0}条,耗时{1}秒", K, dateVal), "提示", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)
                        {
                           // Directory.Delete(deletepath, true);//删除解压文件
                            //Directory.Delete(mediapath, true);//删除上传的压缩包文件
                            Dispose();
                            Application.Exit();
                        }
                    }
                }
            }
            /// <summary>
            /// 将照片转为byte[]
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static byte[] ImageToBytes(Image image)
            {
                ImageFormat format = image.RawFormat;
                using (MemoryStream ms = new MemoryStream())
                {
                    image.Save(ms, format);
                    return ms.ToArray();
                }
            }
            /// <summary> 
            /// 解压缩 
            /// </summary> 
            /// <param name="file">待解压文件名(包含物理路径)</param> 
            /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param> 
            public static bool UnpackFiles(string file, string dir)
            {
                try
                {
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    //使用ZipInputStream需要引入ICSharpCode.SharpZipLib
                    ZipInputStream s = new ZipInputStream(File.OpenRead(file));
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (directoryName != String.Empty)
                        {
                            Directory.CreateDirectory(dir + directoryName);
                        }
                        if (fileName != String.Empty)
                        {
                            FileStream streamWriter = File.Create(dir + theEntry.Name);
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            streamWriter.Close();
                        }
                    }
                    s.Close();
                    return true;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            private List<string> GetAllFiles(string path)
            {
                DirectoryInfo dInfo = new DirectoryInfo(path);
                List<string> list = new List<string>();
                //遍历该文件夹
                // GetFloder(dInfo);
                foreach (FileInfo fileitem in dInfo.GetFiles())
                {
                    if (fileitem.Extension == ".png" || fileitem.Extension == ".jpg" || fileitem.Extension == ".PNG" || fileitem.Extension == ".JPG")
                    {
                        list.Add(fileitem.FullName);
                    }
                }
                return list;
    
            }
    
        }
    }

    解压zip所需dll   https://pan.baidu.com/s/13Wrq0vGM6eSLgXHShnn9pw    密码:kz6e

  • 相关阅读:
    对list集合中的对象进行排序(转载)
    关键字的作用
    CocoaPods的 安装 /卸载/升级
    block基本使用和底层
    程序启动 - 类调用的方法
    成员变量修饰词的作用
    宏(define)与常量(const)
    iOS
    监听网络状态
    nil、Nil、NULL与NSNull的区别及应用
  • 原文地址:https://www.cnblogs.com/macT/p/9970851.html
Copyright © 2011-2022 走看看