public async Task <IActionResult> Batchdownload(string urls) { var uploadFilePath = AppDomain.CurrentDomain.BaseDirectory + "BatchDownload"; //判断文件夹是否存在 if (!Directory.Exists(uploadFilePath)) { Directory.CreateDirectory(uploadFilePath); } List <string> urlList = urls.Split(",").ToList(); foreach (var url in urlList) { string filename = ""; DownFile(url, uploadFilePath, ref filename); } string zipPath = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "Downloads.zip"; if (System.IO.File.Exists(zipPath)) { System.IO.File.Delete(zipPath); } SharpZipHelper.GetInstance().Zip(zipPath, uploadFilePath); FileStream ms = new FileStream(zipPath, FileMode.Open); return(File(ms, "application/octet-stream", "Downloads.zip")); }
public void MakeZipFileTest() { string[] _zipFilepathList = new string[3]; _zipFilepathList[0] = @"D:\ToExecelTest_20160901113654.xls"; _zipFilepathList[1] = @"D:\ToExecelTest_20160901132839.xls"; _zipFilepathList[2] = @"D:\ToExecelTest_20160901132842.xls"; string _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 0); bool _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 1); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 2); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 3); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 4); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 5); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 6); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 7); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 8); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); Thread.Sleep(1000); _zipedFilePath = string.Format(@"D:\ToExecelTest_{0}.zip", DateTime.Now.FormatDate(12)); SharpZipHelper.MakeZipFile(_zipFilepathList, _zipedFilePath, 9); _actual = File.Exists(_zipedFilePath); Assert.IsTrue(_actual); }
public void UnMakeZipFileTest() { string _unZipFilePath = @"D:\ToExecelTest_20160901134224.zip"; SharpZipHelper.UnMakeZipFile(_unZipFilePath); bool _actual = File.Exists(@"D:\ToExecelTest_20160901134224\ToExecelTest_20160901113654.xls"); Assert.IsTrue(_actual); }
private void PushFile(string fname, string filename) { MemoryStream ms = SharpZipHelper.UnzipFileToStream(fname); Response.ContentType = "file/" + Path.GetExtension(filename); Response.Clear(); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(filename)); Response.BinaryWrite(ms.ToArray()); ms.Close(); }
/// <summary> /// 客户端Gzip压缩数据解压 /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GzipEncoding(string text) { if (string.IsNullOrWhiteSpace(text)) { return(null); } try { var isoBytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(text); var gZipBytes = SharpZipHelper.GZipDeCompress(isoBytes); text = HttpUtility.UrlDecode(Encoding.Default.GetString(gZipBytes)); return(text); } catch (Exception e) { NLogger.Error(e); } return(null); }
public virtual bool Backup(List <BackupItemEx> items, string backupfile) { try { if (items != null && items.Count > 0) { string dir = AppDomain.CurrentDomain.BaseDirectory; // make sure the capacity is big enough List <string> files = new List <string>(items.Count); int index = 0; int count = 0; AutoResetEvent autoResetEvent = new AutoResetEvent(false); foreach (BackupItemEx bi in items) { if ((bi.BackupObjects == null || bi.BackupObjects.Count == 0) && string.IsNullOrEmpty(bi.DumpQuery)) { continue; } ++count; ThreadPool.QueueUserWorkItem((obj) => { BackupItemEx tmpBi = obj as BackupItemEx; Backup(tmpBi); if (File.Exists(tmpBi.BackupFileName)) { FileInfo fi = new FileInfo(tmpBi.BackupFileName); if (fi.Length > 2) { lock (this) //need synchronization block { files.Add(tmpBi.BackupFileName); } } else { FileDirectoryOperate.DeleteFileWithTime(tmpBi.BackupFileName); } } Interlocked.Increment(ref index); if (index == count) { autoResetEvent.Set(); } try { if (worker != null && worker.WorkerReportsProgress && worker.IsBusy) { worker.ReportProgress(index * 100 / items.Count); } } catch { } }, bi); } autoResetEvent.WaitOne(60000, false); if (files.Count == 0) { return(false); } if (worker != null && worker.WorkerReportsProgress && worker.IsBusy) { worker.ReportProgress(50, string.Format("Back up database:", backupfile)); } SharpZipHelper.ZipMultiFiles(files.ToArray(), backupfile, DatabaseCommon.ZipPwd, 3, worker); foreach (string file in files) { FileDirectoryOperate.DeleteFileWithTime(file); } return(true); } else { return(false); } } catch (Exception ex) { LogHelper.Log(ex); return(false); } }
public override bool Backup(string dbName, string backupfile, bool packing) { try { using (SqlConnection conn = new SqlConnection(GetConnectionString(true))) { conn.Open(); string dir = string.Empty; dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Temp\"); if (!Directory.Exists(dir)) { FileDirectoryOperate.CreateDirectoryEx(dir); } else { // SQL Server backup file foreach (string s in Directory.GetFiles(dir, "*.bak")) { FileDirectoryOperate.DeleteFileWithTime(s); } } #region File list to packup string tempFile = Path.Combine(dir, dbName + ".bak"); List <string> files = new List <string>(2); files.Add(tempFile); #endregion DbCommand cmd = conn.CreateCommand(); cmd.CommandText = string.Format("backup database {0} to disk = '{1}'", dbName, tempFile); cmd.CommandTimeout = 600; cmd.ExecuteNonQuery(); if (!File.Exists(tempFile)) { return(false); } if (worker != null && worker.WorkerReportsProgress) { if (packing) { worker.ReportProgress(0, string.Format("DataBase Packing Up Files......", backupfile)); } else { worker.ReportProgress(100); } } if (!packing) { return(true); } SharpZipHelper.ZipMultiFiles(files.ToArray(), backupfile, DatabaseCommon.ZipPwd, 6, worker); foreach (string file in files) { FileDirectoryOperate.DeleteFileWithTime(file); } return(true); } } catch (Exception) { return(false); } }
private void UpLoadFile(int FileID) { string path = WebHelper.GetAttachUpDir(); SharpZipHelper.ZipFile(FileUp.FileContent, path + FileID.ToString() + ".xmx"); }