主要两个问题:
1、获取zip文件列表,找出所需的文件
2、解压缩含所需文件的zip档案到临时文件夹
3、打开所需的文本文件,找到最后一行记录我想可能是zip不会吧
可以使用SharpZipLib来处理zip文件,见下,当然,调用winrar命令行参数处理也是可以的,不过winrar可是有版权的
http://www.icsharpcode.net/OpenSource/SharpZipLib/public static void List(string zipFile)
{
if (File.Exists(zipFile))
{
ZipFile zip = new ZipFile(zipFile); foreach (ZipEntry entry in zip)
{
if (entry.IsFile)
Console.WriteLine(entry.Name);
}
}
else
Console.WriteLine(zipFile + " 不存在");
}
List("C:\\Downloads\\test.zip");解压缩:
public static void Uncompress(string zipFileName, string targetPath)
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)))
{
// 若目的路径不存在,则先建立路径
DirectoryInfo di = new DirectoryInfo(targetPath); if (!di.Exists)
di.Create(); ZipEntry theEntry; // 解压缩
while ((theEntry = s.GetNextEntry()) != null)
{
int size = 2048;
byte[] data = new byte[2048]; Console.WriteLine("正在解压缩: " + GetBasename(theEntry.Name)); // 写文件
using (FileStream fs = new FileStream(di.FullName + "\\" + GetBasename(theEntry.Name), FileMode.Create))
{
while (true)
{
size = s.Read(data, 0, data.Length); if (size > 0)
fs.Write(data, 0, size);
else
break;
} }
}
}
}
public static string GetBasename(string fullName)
{
string result;
int lastBackSlash = fullName.LastIndexOf("\\");
result = fullName.Substring(lastBackSlash + 1);" return result;
}
调用Upcompress ,将test.zip 解压缩到 C:\Downloads\test 目录下:
Uncompress("C:\\Downloads\\test.zip", "C:\\Downloads\\test");
1、获取zip文件列表,找出所需的文件
2、解压缩含所需文件的zip档案到临时文件夹
3、打开所需的文本文件,找到最后一行记录我想可能是zip不会吧
可以使用SharpZipLib来处理zip文件,见下,当然,调用winrar命令行参数处理也是可以的,不过winrar可是有版权的
http://www.icsharpcode.net/OpenSource/SharpZipLib/public static void List(string zipFile)
{
if (File.Exists(zipFile))
{
ZipFile zip = new ZipFile(zipFile); foreach (ZipEntry entry in zip)
{
if (entry.IsFile)
Console.WriteLine(entry.Name);
}
}
else
Console.WriteLine(zipFile + " 不存在");
}
List("C:\\Downloads\\test.zip");解压缩:
public static void Uncompress(string zipFileName, string targetPath)
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)))
{
// 若目的路径不存在,则先建立路径
DirectoryInfo di = new DirectoryInfo(targetPath); if (!di.Exists)
di.Create(); ZipEntry theEntry; // 解压缩
while ((theEntry = s.GetNextEntry()) != null)
{
int size = 2048;
byte[] data = new byte[2048]; Console.WriteLine("正在解压缩: " + GetBasename(theEntry.Name)); // 写文件
using (FileStream fs = new FileStream(di.FullName + "\\" + GetBasename(theEntry.Name), FileMode.Create))
{
while (true)
{
size = s.Read(data, 0, data.Length); if (size > 0)
fs.Write(data, 0, size);
else
break;
} }
}
}
}
public static string GetBasename(string fullName)
{
string result;
int lastBackSlash = fullName.LastIndexOf("\\");
result = fullName.Substring(lastBackSlash + 1);" return result;
}
调用Upcompress ,将test.zip 解压缩到 C:\Downloads\test 目录下:
Uncompress("C:\\Downloads\\test.zip", "C:\\Downloads\\test");
解决方案 »
- c# 中out参数
- C#
- ajax 的全国地址三级 选择菜单 无刷新 会显示Method Error 500 请大家帮忙调试一下
- 关于用table拍版的问题
- 问一个UDPCLIENT的问题,有兴趣的进啊!
- 请教一个SQL Server 查询的性能问题
- 未处理的“System.IndexOutOfRangeException”类型的异常出现在 system.data.dll 中。
- 求助:在.Net中如何实现动态数据交换?
- 关于ConfigurationManager这个类。
- 用socket写的一个程序,客户端send一个长字符串,服务端Recieve到的字符串被截成了几段,每段有25个字符,为什么?
- windows应用程序中获取控制台输出信息
- 请教:如何在子窗口中使用父窗口中的listview控件?!
StreamReader sr = File.OpenText(fname);//打开文件
StreamReader srReadLine = new StreamReader(fname, System.Text.Encoding.GetEncoding("GB2312"));//转编码方式
string line;
line = sr.ReadLine();//按行读取
if (line != null)
{
while (line != null)
{
line = sr.ReadLine();
if(line != null)
{
i++;
if (i == line.Length)//循环所有行
{
break;
}
}
}
sr.Close();
}
上面同学给出的代码就可以了。
简洁一点的写法就是
StreamReader sr = File.OpenText(fname);//打开文件
StreamReader srReadLine = new StreamReader(fname, System.Text.Encoding.GetEncoding("GB2312"));//转编码方式
string line;
string result;
do
{
line=srReadLine.ReadLine();//读行
if(line!=null)
result=line;//循环到最后,line肯定为空,那么之前保存的一行就是最后一行了,用result来记录就可以了
}
while(line!=null);//下面就可以分最后一行了
string []f1=result.Split(" ");
//f1[0]就是所需嘛上面的没调试,可能会有些问题如果考虑效率,我想可以用seek来定位到最后,找到前一个或后一个回车就可以了吧。
应该是
string []f1=result.Split(' ');
空格
说的什么意思?没明白
20080702_0906-0203-01.ZIP中是否包含有a11008.txt,如果没有,查找20080702_0906-0216-01.ZIP
逻辑有问题吧?
表table中列出的所有zip文件进行逐个搜索,比如看20080702_0906-0203-01.ZIP中是否包含有a11008.txt文件,如果有,则做取最后一行的第一列数据,存到下面的表中:
网点号 1日 2日 3日 4日 5日 6日 .....31日
0200
0201
0202
0203 5
0212 8
2016 12
如果没有,那么继续查找其他的ZIP文件中是否有a11008.txt文件,只要有a11008.txt文件,那么就取数并存放。
(因为有的zip压缩包里没有a11008.txt文件)
{
…………
//存在该文件执行
}
else if (File.Exists("*.ZIP\\a11008.txt") == true)
{}是要这样的东西????
{
解压缩函数
判断文件是否存在
如果存在执行你的提取数据函数
如果不存在继续FOR循环,
不知道我说对了没有?
}