hyytaojunming 发表于 2015-5-26 11:03:08

如何直接处理FTP服务器上的压缩文件?

  我最近要写一个供有相关权限的管理人员查询大额资金明细的程序,界面如下:


所需的数据文件是放在报表服务器上,每天一个压缩文件,该压缩文件中除了所需的储蓄流水账文件外,还有很多其他的文件。如果先把该压缩文件从报表服务器下载到应用服务器上,再进行解压缩处理的话,一是多下载了该压缩文件中我们不需要的其他文件,二是还必须在应用服务器上建立以SessionID等方法标识的临时文件,以免其他用户也在进行查询时文件名冲突,三是使用完毕后还必须删除该临时文件。
我的处理方法是如下:
using (ZipInputStream zs = Zip.GetZipInputStream((new FtpClient(Pub.Ftp1Uri, Pub.Ftp1User, Pub.Ftp1Pswd)).
      GetDownloadStream(Path.Combine(Pub.Ftp1EPath, zipFileName)), binFileName, out size))
      {
      if (size % Pub.P_R2 != 0) throw new ApplicationException("文件长度错: " + binFileName);
      byte [] bs = new byte;
      long recs = size / Pub.P_R2;
      for (long rec = 0; rec < recs; rec++)
      {
          Zip.ReadStream(zs, bs);
          ...
      }
首先,用 FtpClient.GetDownloadStream() 方法得到一个对应于FTP服务器上文件的Stream,然后把这个Stream传给Zip.GetZipInputStream()方法,得到一个ZipInputStream,然后使用Zip.ReadStream()方法一行一行读取储蓄流水账文件到byte[]中去,这样就取得了我们所需的数据,就象储蓄流水账文件就存放在本地硬盘上一样,避免了下载文件和解压文件。具体代码如下:

1using System;
2using System.IO;
3using System.Net;
4
5namespace Skyiv.Util
6{
7sealed class FtpClient
8{
9    Uri uri;
10    string userName;
11    string password;
12   
13    public FtpClient(string uri, string userName, string password)
14    {
15      this.uri = new Uri(uri);
16      this.userName = userName;
17      this.password = password;
18    }
19   
20    public Stream GetDownloadStream(string sourceFile)
21    {
22      Uri downloadUri = new Uri(uri, sourceFile);
23      if (downloadUri.Scheme != Uri.UriSchemeFtp) throw new ArgumentException("URI is not an FTP site");
24      FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
25      ftpRequest.Credentials = new NetworkCredential(userName, password);
26      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
27      return ((FtpWebResponse)ftpRequest.GetResponse()).GetResponseStream();
28    }
29   
30    /**//*public */void Download(string sourceFile, string destinationFile)
31    {
32      using (Stream reader = GetDownloadStream(sourceFile))
33      {
34      using (BinaryWriter bw = new BinaryWriter(new FileStream(destinationFile, FileMode.Create)))
35      {
36          for (;;)
37          {
38            int c = reader.ReadByte();
39            if (c == -1) break;
40            bw.Write((byte)c);
41          }
42      }
43      }
44    }
45}
46}
47
1using System;
2using System.IO;
3using ICSharpCode.SharpZipLib.Zip;
4
5namespace Skyiv.Util
6{
7static class Zip
8{
9    public static void ReadStream(Stream inStream, byte [] bs)
10    {
11      int offset = 0;
12      int count = bs.Length;
13      do
14      {
15      int size = inStream.Read(bs, offset, count);
16      if (size0);
20      if (count != 0) throw new ApplicationException("Zip.ReadStream(): 读输入流错");
21    }
22   
23    public static ZipInputStream GetZipInputStream(string zipFileName, string fileName, out long fileLength)
24    {
25      return GetZipInputStream(File.OpenRead(zipFileName), fileName, out fileLength);
26    }
27   
28    public static ZipInputStream GetZipInputStream(Stream zipStream, string fileName, out long fileLength)
29    {
30      ZipInputStream zs = new ZipInputStream(zipStream);
31      ZipEntry theEntry;
32      while ((theEntry = zs.GetNextEntry()) != null)
33      {
34      if (Path.GetFileName(theEntry.Name) != fileName) continue;
35      fileLength = theEntry.Size;
36      return zs;
37      }
38      fileLength = -1;
39      return null;
40    }
41
42    /**//*public */static void UnzipFile(string zipFile, string baseStr, params string [] fileList)
43    {
44      UnzipFile(File.OpenRead(zipFile), baseStr, fileList);
45    }
46   
47    /**//*public */static void UnzipFile(Stream zipStream, string baseStr, params string [] fileList)
48    {
49      using (ZipInputStream zs = new ZipInputStream(zipStream))
50      {
51      ZipEntry theEntry;
52      byte[] data = new byte;
53      while ((theEntry = zs.GetNextEntry()) != null) {
54          if (Array.IndexOf(fileList, Path.GetFileName(theEntry.Name)) < 0) continue;
55          using (FileStream sw = new FileStream(baseStr+Path.GetFileName(theEntry.Name), FileMode.Create))
56          {
57            while (true) {
58            int size = zs.Read(data, 0, data.Length);
59            if (size
页: [1]
查看完整版本: 如何直接处理FTP服务器上的压缩文件?