/// <summary> /// Zip压缩一个文件至压缩文件 /// </summary> /// <param name="srcFile"></param> /// <param name="destFile"></param> /// <returns></returns> public static string ZipFromFile(string srcFile, string destFile) { ZipArchive archive = CreateZipArchive(destFile, true); AbstractFile source = new DiskFile(srcFile); source.CopyTo(archive, true); return(destFile); }
/// <summary> /// 上传文件 /// </summary> /// <param name="ftpAddress"></param> /// <param name="remoteFilename"></param> /// <param name="localFilename"></param> /// <param name="resumeOperation"></param> public bool UploadFile(string ftpAddress, string localFilename, string remoteFilename, bool resumeOperation) { FtpSiteData siteData = ParseFtpAddress(ftpAddress); if (siteData == null) { throw new ArgumentException("Invalid ftp address format!"); } using (FtpConnection connection = new FtpConnection(siteData.Host, siteData.Port, siteData.UserName, siteData.Password)) { SetConnection(connection); AbstractFolder remoteFolder = new FtpFolder(connection); AbstractFile remoteFile = remoteFolder.GetFile(remoteFilename); AbstractFile localFile = new DiskFile(localFilename); if (!resumeOperation || !remoteFile.Exists || remoteFile.Size > localFile.Size) { localFile.CopyTo(remoteFile, true); } else if (remoteFile.Size == localFile.Size) { return(true); } else if (remoteFile.Size < localFile.Size) { byte[] buf = new byte[1024]; int cnt = -1; using (System.IO.Stream remoteStream = remoteFile.OpenWrite(false)) { using (System.IO.Stream localStream = localFile.OpenRead()) { localStream.Seek(remoteFile.Size, System.IO.SeekOrigin.Begin); // can't seek. OpenWrite如果不overwrite自动append //remoteStream.Seek(0, System.IO.SeekOrigin.End); do { cnt = localStream.Read(buf, 0, buf.Length); remoteStream.Write(buf, 0, cnt); } while (cnt == buf.Length); } } } return(true); } //FtpClient client = LoginFtp(ftpAddress); //client.SendFile(localFilename, remoteFilename); }
private void btnStart_Click(object sender, EventArgs e) { btnStart.Enabled = false; try { Xceed.Ftp.Licenser.LicenseKey = "FTN42-K40Z3-DXCGS-PYGA"; data.Columns.Clear(); data.Rows.Clear(); data.Columns.Add("name", "Точка"); data.Columns.Add("rezult", "Результат"); data.Columns[0].Width = 200; data.Columns[1].Width = 350; cn = new SqlConnection(setting.Connection_string); cn.Open(); cmd = new SqlCommand("SELECT * FROM [place];", cn); DataTable tbl = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(tbl); pb.Minimum = 0; pb.Maximum = tbl.Rows.Count; pb.Value = 0; foreach (DataRow r in tbl.Rows) { try { using (FtpConnection connection = new FtpConnection( r["server"].ToString().Trim(), r["username"].ToString().Trim(), r["password"].ToString().Trim())) { connection.Timeout = 10; string tmp = Guid.NewGuid().ToString(); StreamWriter w = new StreamWriter(System.IO.Path.GetTempPath() + tmp); w.Write(tmp); w.Close(); connection.Encoding = Encoding.GetEncoding(1251); DiskFile source = new DiskFile(System.IO.Path.GetTempPath() + tmp); string ftp_to = r["path"].ToString().Trim(); if (ftp_to.Substring(0, 1) == "/") ftp_to = ftp_to.Substring(1); FtpFolder destination = new FtpFolder(connection, ftp_to); source.CopyTo(destination, true); Thread.Sleep(2000); FtpFile remote = new FtpFile(connection, ftp_to + tmp); remote.Delete(); } data.Rows.Add(new string[] { r["name"].ToString().Trim(), "ok" }); } catch (Exception ex) { data.Rows.Add(new string[] { r["name"].ToString().Trim(), "ошибка: " + ex.Message }); } finally { pb.Value++; Application.DoEvents(); } } } catch { } finally { btnStart.Enabled = true; MessageBox.Show("ok"); } }
/// <summary> /// Zip压缩一个文件至压缩文件 /// </summary> /// <param name="srcFile"></param> /// <param name="destFile"></param> /// <returns></returns> public static string ZipFromFile(string srcFile, string destFile) { ZipArchive archive = CreateZipArchive(destFile, true); AbstractFile source = new DiskFile(srcFile); source.CopyTo(archive, true); return destFile; }