压缩文件包含盘符问题

xiangchengboy 2011-02-22 10:34:20
[code=C]#/// <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);
}
}[/code]
用以上方法实现压缩文件,问题是:当文件存在根盘符下面时,压缩文件中包含一个盘符文件夹(E_),打开该文件夹才能找到文件。
...全文
170 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangchengboy 2011-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sq_zhuyi 的回复:]

C#
Upload(zippath,zipfilename);//在哪

[/Quote][code=C]# /// <summary>
/// 文件上传
/// </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"));
}
}[/code]
wuyq11 2011-02-22
  • 打赏
  • 举报
回复
直接压缩文件
路人乙e 2011-02-22
  • 打赏
  • 举报
回复
[code=C]#
Upload(zippath,zipfilename);//在哪
[/code]
xiangchengboy 2011-02-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qldsrx 的回复:]

根目录不是一样操作的吗?没看出有什么问题,你有在陈述问题吗?
[/Quote]我不需要这个盘符,打开直接是文件就好,如果不是放在根目录下则不会有这个问题
qldsrx 2011-02-22
  • 打赏
  • 举报
回复
根目录不是一样操作的吗?没看出有什么问题,你有在陈述问题吗?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧