1. 程式人生 > >多媒體照片打成壓縮包上傳 (二)

多媒體照片打成壓縮包上傳 (二)

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