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

我最近要写一个供有相关权限的管理人员查询大额资金明细的程序,界面如下:
FTP-01.PNG
ftp-02.PNG
所需的数据文件是放在报表服务器上,每天一个压缩文件,该压缩文件中除了所需的储蓄流水账文件外,还有很多其他的文件。如果先把该压缩文件从报表服务器下载到应用服务器上,再进行解压缩处理的话,一是多下载了该压缩文件中我们不需要的其他文件,二是还必须在应用服务器上建立以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[Pub.P_R2];
        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[]中去,这样就取得了我们所需的数据,就象储蓄流水账文件就存放在本地硬盘上一样,避免了下载文件和解压文件。具体代码如下:

 1 None.gif using  System;
 2 None.gif using  System.IO;
 3 None.gif using  System.Net;
 4 None.gif
 5 None.gif namespace  Skyiv.Util
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif  sealed class FtpClient
 8ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 9InBlock.gif    Uri uri;
10InBlock.gif    string userName;
11InBlock.gif    string password;
12InBlock.gif    
13InBlock.gif    public FtpClient(string uri, string userName, string password)
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif      this.uri = new Uri(uri);
16InBlock.gif      this.userName = userName;
17InBlock.gif      this.password = password;
18ExpandedSubBlockEnd.gif    }

19InBlock.gif    
20InBlock.gif    public Stream GetDownloadStream(string sourceFile)
21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
22InBlock.gif      Uri downloadUri = new Uri(uri, sourceFile);
23InBlock.gif      if (downloadUri.Scheme != Uri.UriSchemeFtp) throw new ArgumentException("URI is not an FTP site");
24InBlock.gif      FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
25InBlock.gif      ftpRequest.Credentials = new NetworkCredential(userName, password);
26InBlock.gif      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
27InBlock.gif      return ((FtpWebResponse)ftpRequest.GetResponse()).GetResponseStream();
28ExpandedSubBlockEnd.gif    }

29InBlock.gif    
30ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*public */void Download(string sourceFile, string destinationFile)
31ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
32InBlock.gif      using (Stream reader = GetDownloadStream(sourceFile))
33ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
34InBlock.gif        using (BinaryWriter bw = new BinaryWriter(new FileStream(destinationFile, FileMode.Create)))
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif          for (;;)
37ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
38InBlock.gif            int c = reader.ReadByte();
39InBlock.gif            if (c == -1break;
40InBlock.gif            bw.Write((byte)c);
41ExpandedSubBlockEnd.gif          }

42ExpandedSubBlockEnd.gif        }

43ExpandedSubBlockEnd.gif      }

44ExpandedSubBlockEnd.gif    }

45ExpandedSubBlockEnd.gif  }

46ExpandedBlockEnd.gif}

47 None.gif
 1 None.gif using  System;
 2 None.gif using  System.IO;
 3 None.gif using  ICSharpCode.SharpZipLib.Zip;
 4 None.gif
 5 None.gif namespace  Skyiv.Util
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif  static class Zip
 8ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 9InBlock.gif    public static void ReadStream(Stream inStream, byte [] bs)
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif      int offset = 0;
12InBlock.gif      int count = bs.Length;
13InBlock.gif      do 
14ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
15InBlock.gif        int size = inStream.Read(bs, offset, count);
16InBlock.gif        if (size <= 0break;
17InBlock.gif        offset += size;
18InBlock.gif        count -= size;
19ExpandedSubBlockEnd.gif      }
 while (count > 0);
20InBlock.gif      if (count != 0throw new ApplicationException("Zip.ReadStream(): 读输入流错");
21ExpandedSubBlockEnd.gif    }

22InBlock.gif    
23InBlock.gif    public static ZipInputStream GetZipInputStream(string zipFileName, string fileName, out long fileLength)
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif      return GetZipInputStream(File.OpenRead(zipFileName), fileName, out fileLength);
26ExpandedSubBlockEnd.gif    }

27InBlock.gif    
28InBlock.gif    public static ZipInputStream GetZipInputStream(Stream zipStream, string fileName, out long fileLength)
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30InBlock.gif      ZipInputStream zs = new ZipInputStream(zipStream);
31InBlock.gif      ZipEntry theEntry;
32InBlock.gif      while ((theEntry = zs.GetNextEntry()) != null
33ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
34InBlock.gif        if (Path.GetFileName(theEntry.Name) != fileName) continue;
35InBlock.gif        fileLength = theEntry.Size;
36InBlock.gif        return zs;
37ExpandedSubBlockEnd.gif      }

38InBlock.gif      fileLength = -1;
39InBlock.gif      return null;
40ExpandedSubBlockEnd.gif    }

41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*public */static void UnzipFile(string zipFile, string baseStr, params string [] fileList)
43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
44InBlock.gif      UnzipFile(File.OpenRead(zipFile), baseStr, fileList);
45ExpandedSubBlockEnd.gif    }

46InBlock.gif    
47ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*public */static void UnzipFile(Stream zipStream, string baseStr, params string [] fileList)
48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
49InBlock.gif      using (ZipInputStream zs = new ZipInputStream(zipStream))
50ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
51InBlock.gif        ZipEntry theEntry;
52InBlock.gif        byte[] data = new byte[4096];
53ExpandedSubBlockStart.gifContractedSubBlock.gif        while ((theEntry = zs.GetNextEntry()) != nulldot.gif{
54InBlock.gif          if (Array.IndexOf(fileList, Path.GetFileName(theEntry.Name)) < 0continue;
55InBlock.gif          using (FileStream sw = new FileStream(baseStr+Path.GetFileName(theEntry.Name), FileMode.Create))
56ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
57ExpandedSubBlockStart.gifContractedSubBlock.gif            while (truedot.gif{
58InBlock.gif              int size = zs.Read(data, 0, data.Length);
59InBlock.gif              if (size <= 0break;
60InBlock.gif              sw.Write(data, 0, size);
61ExpandedSubBlockEnd.gif            }

62ExpandedSubBlockEnd.gif          }

63ExpandedSubBlockEnd.gif        }

64ExpandedSubBlockEnd.gif      }

65ExpandedSubBlockEnd.gif    }

66ExpandedSubBlockEnd.gif  }

67ExpandedBlockEnd.gif}

68 None.gif
 1 None.gif      void  MakeData(DateTime workDate,  short  brno,  short  currtype,  decimal  startAmt)
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif      Pub.SetNOVA(workDate);
 4InBlock.gif      string zipFileName = string.Format("e-{0:yyyyMMdd}-{1:D4}-0000{2}.zip", workDate, Pub.Zone, Pub.IsNOVAv12 ? "-2" : "");
 5InBlock.gif      string binFileName = string.Format("e-{0:yyyyMMdd}-pfsjnl.bin", workDate);
 6InBlock.gif      long size;
 7InBlock.gif      DataTable dt = MakeDataTable();
 8InBlock.gif      using (ZipInputStream zs = Zip.GetZipInputStream((new FtpClient(Pub.Ftp1Uri, Pub.Ftp1User, Pub.Ftp1Pswd)).
 9InBlock.gif        GetDownloadStream(Path.Combine(Pub.Ftp1EPath, zipFileName)), binFileName, out size))
10ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
11InBlock.gif        if (size % Pub.P_R2 != 0throw new ApplicationException("文件长度错: " + binFileName);
12InBlock.gif        byte [] bs = new byte[Pub.P_R2];
13InBlock.gif        long recs = size / Pub.P_R2;
14InBlock.gif        for (long rec = 0; rec < recs; rec++)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif          Zip.ReadStream(zs, bs);
17InBlock.gif          if (Etc.Encode.GetString(bs,Pub.P_R2_RevTran,Pub.L_Revtran) != "0"continue// 反交易标志
18InBlock.gif          for (int i = 0; i < 2; i++)
19ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
20InBlock.gif            bool isDebit = (i == 0);
21InBlock.gif            if (int.Parse(Etc.Encode.GetString(bs,(isDebit?Pub.P_R2_Drcurr:Pub.P_R2_Crcurr),Pub.L_Curr)) != currtype) continue;
22InBlock.gif            decimal amt0 = decimal.Parse(Etc.Encode.GetString(bs,(isDebit?Pub.P_R2_Amount1:Pub.P_R2_Amount2),Pub.L_Bal)) / 100;
23InBlock.gif            if (Math.Abs(amt0) < startAmt) continue;
24InBlock.gif            string account = Etc.Encode.GetString(bs,(isDebit?Pub.P_R2_Draccno:Pub.P_R2_Craccno),Pub.L_Accno);
25InBlock.gif            if (!DbBranch.IsMyAccount(account, brno)) continue;
26InBlock.gif            string customer = Etc.Encode.GetString(bs,Pub.P_R2_Notes1,Pub.L_Notes1).Trim('@'' ');
27InBlock.gif            short nodeno = short.Parse(Etc.Encode.GetString(bs,Pub.P_R2_Brno,Pub.L_Brno));
28InBlock.gif            int code = int.Parse(Etc.Encode.GetString(bs,Pub.P_R2_Trxcode,Pub.L_Trxcode));
29InBlock.gif            DataRow dr = dt.NewRow();
30InBlock.gif            dr["No"= nodeno;
31InBlock.gif            dr["Name"= DbBranch.GetBrief(DbBranch.IsAllNode(brno), nodeno);
32InBlock.gif            dr["Teller"= int.Parse(Etc.Encode.GetString(bs,Pub.P_R2_Teller,Pub.L_Teller));
33InBlock.gif            dr["Account"= IcbcEtc.FormatAccount19(account);
34InBlock.gif            dr["User"= customer;
35InBlock.gif            dr["Flag"= (isDebit ? "" : "");
36InBlock.gif            dr["Amt"= amt0;
37InBlock.gif            dr["Memo"= Etc.Encode.GetString(bs,Pub.P_R2_Cashnote,Pub.L_Cashnote);
38InBlock.gif            dr["Code"= code;
39InBlock.gif            dr["TrName"= DbTrxCode.GetNewName(code);
40InBlock.gif            dt.Rows.Add(dr);
41ExpandedSubBlockEnd.gif          }

42ExpandedSubBlockEnd.gif        }

43ExpandedSubBlockEnd.gif      }

44InBlock.gif      DataView dv = dt.DefaultView;
45InBlock.gif      dv.Sort = "Flag DESC, Amt DESC";
46InBlock.gif      dgMain.DataSource = dv;
47InBlock.gif      dgMain.DataBind();
48ExpandedBlockEnd.gif    }

49 None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值