#/// <summary>
/// 压缩指定目录下指定文件(包括子目录下的文件)
/// </summary>
/// <param >args[0]为你要压缩的目录所在的路径
/// 例如:D:\\temp\\ (注意temp 后面加 \\ 但是你写程序的时候怎么修改都可以)</param>
/// <param >args[1]为压缩后的文件名及其路径
/// 例如:D:\\temp.zip</param>
/// <param >文件过滤, 例如*.xml,这样只压缩.xml文件.</param>
public bool ZipFileMain(string zippath, string zipfilename, string fileFilter)
{ //string filenames = Directory.GetFiles(args[0]); try
{
Crc32 crc = new Crc32();
ZipOutputStream outStream = new ZipOutputStream(File.Create(zippath + zipfilename)); outStream.SetLevel(6); // 0 - store only to 9 - means best compression DirectoryInfo di = new DirectoryInfo(zippath); FileInfo[] files = di.GetFiles(fileFilter);
cutStr = zippath.Trim();
//压缩这个目录下的所有文件
writeStream(ref outStream, files, crc);
//压缩这个目录下子目录及其文件
//direct(di, ref s, crc); outStream.Finish();
outStream.Close();
}
catch (Exception exp)
{
return false; }
Upload(zippath,zipfilename); return true;
} /// <summary>
/// 文件压缩成zip格式
/// </summary>
/// <param name="outStream"></param>
/// <param name="files"></param>
/// <param name="crc"></param>
private void writeStream(ref ZipOutputStream outStream, FileInfo[] files, Crc32 crc)
{
foreach (FileInfo fi in files)
{
//string fifn = fi.FullName;
FileStream fs = fi.OpenRead(); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//ZipEntry entry = new ZipEntry(file); Path.GetFileName(file)
string file = fi.FullName;
file = file.Replace(cutStr, ""); ZipEntry entry = new ZipEntry(file); entry.DateTime = DateTime.Now; entry.Size = fs.Length;
fs.Close(); crc.Reset();
crc.Update(buffer); entry.Crc = crc.Value; outStream.PutNextEntry(entry); outStream.Write(buffer, 0, buffer.Length);
}
}
用以上方法实现压缩文件,问题是:当文件存在根盘符下面时,压缩文件中包含一个盘符文件夹(E_),打开该文件夹才能找到文件。
/// 压缩指定目录下指定文件(包括子目录下的文件)
/// </summary>
/// <param >args[0]为你要压缩的目录所在的路径
/// 例如:D:\\temp\\ (注意temp 后面加 \\ 但是你写程序的时候怎么修改都可以)</param>
/// <param >args[1]为压缩后的文件名及其路径
/// 例如:D:\\temp.zip</param>
/// <param >文件过滤, 例如*.xml,这样只压缩.xml文件.</param>
public bool ZipFileMain(string zippath, string zipfilename, string fileFilter)
{ //string filenames = Directory.GetFiles(args[0]); try
{
Crc32 crc = new Crc32();
ZipOutputStream outStream = new ZipOutputStream(File.Create(zippath + zipfilename)); outStream.SetLevel(6); // 0 - store only to 9 - means best compression DirectoryInfo di = new DirectoryInfo(zippath); FileInfo[] files = di.GetFiles(fileFilter);
cutStr = zippath.Trim();
//压缩这个目录下的所有文件
writeStream(ref outStream, files, crc);
//压缩这个目录下子目录及其文件
//direct(di, ref s, crc); outStream.Finish();
outStream.Close();
}
catch (Exception exp)
{
return false; }
Upload(zippath,zipfilename); return true;
} /// <summary>
/// 文件压缩成zip格式
/// </summary>
/// <param name="outStream"></param>
/// <param name="files"></param>
/// <param name="crc"></param>
private void writeStream(ref ZipOutputStream outStream, FileInfo[] files, Crc32 crc)
{
foreach (FileInfo fi in files)
{
//string fifn = fi.FullName;
FileStream fs = fi.OpenRead(); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//ZipEntry entry = new ZipEntry(file); Path.GetFileName(file)
string file = fi.FullName;
file = file.Replace(cutStr, ""); ZipEntry entry = new ZipEntry(file); entry.DateTime = DateTime.Now; entry.Size = fs.Length;
fs.Close(); crc.Reset();
crc.Update(buffer); entry.Crc = crc.Value; outStream.PutNextEntry(entry); outStream.Write(buffer, 0, buffer.Length);
}
}
用以上方法实现压缩文件,问题是:当文件存在根盘符下面时,压缩文件中包含一个盘符文件夹(E_),打开该文件夹才能找到文件。
Upload(zippath,zipfilename);//在哪
/// 文件上传
/// </summary>
/// <param name="filepath"></param>
/// <param name="filename"></param> private void Upload(string filepath, string zipFilename)
{
//WebServiceProxy.WebServiceProxy webs = new WebServiceProxy.WebServiceProxy("http://xueweichao.gicp.net:8080/des-labour/services/labourService?wsdl");
FTPWebServiceUpLoad.ftpInfo fi = new FTPWebServiceUpLoad.ftpInfo();
LabourServiceImplService labour = GetLabour(); //Settings sdf= ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); try
{
fi = labour.getFTPInfo(userid, userpwd);
}
catch (Exception exp)
{ MessageBox.Show(exp.ToString());
} string ftpServerIP = fi.ip;
string ftpUserID = fi.username;
string ftpPassword = fi.password;
string ftpPort = fi.port.ToString(); //ftpServerIP = "10.1.46.104";
//ftpUserID = "aamm";
//ftpPassword = "6512191";
//ftpPort = "21"; FileInfo fileInf = new FileInfo(filepath + zipFilename); if (!fileInf.Exists)
{
MessageBox.Show("文件不存在!请确认后重新上报!");
Application.Exit();
} string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false; // 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型
reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb
int buffLength = 2048; byte[] buff = new byte[buffLength];
int contentLen; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength);
} // 关闭两个流
strm.Close();
fs.Close(); string result = string.Empty;
try
{
result = labour.uploadSuccess(userid, userpwd, zipFilename);
}
catch (Exception exp)
{
result = "上报失败!";
throw exp;
//MessageBox.Show(msg);
//Application.Exit();
} //MessageBox.Show(msg); //创建一个文件流,用以写入或者创建一个StreamWriter
FileStream filestr = new FileStream(filepath + fileName.Replace("." ,"Result."), FileMode.Create, FileAccess.Write);
StreamWriter streamwr = new StreamWriter(filestr,Encoding.GetEncoding("GB2312"));
streamwr.Flush();
// 使用StreamWriter来往文件中写入内容
streamwr.BaseStream.Seek(0, SeekOrigin.Begin); if (result == "1")
{
streamwr.Write("true");
}
else
{
streamwr.Write("false");
} streamwr.Flush();
streamwr.Close();
filestr.Close(); }
catch (Exception ex)
{
throw ex;
//创建一个文件流,用以写入或者创建一个StreamWriter
FileStream filestr = new FileStream(filepath + fileName.Replace(".", "Result."), FileMode.Create, FileAccess.Write);
StreamWriter streamwr = new StreamWriter(filestr, Encoding.GetEncoding("GB2312"));
streamwr.Flush();
// 使用StreamWriter来往文件中写入内容
streamwr.BaseStream.Seek(0, SeekOrigin.Begin);
streamwr.Write("false");
streamwr.Flush();
streamwr.Close();
filestr.Close();
}
finally
{
FilePicDelete(filepath + zipFilename);
FilePicDelete(filepath + zipFilename.Replace("zip", "txt"));
}
}