示例#1
0
        //public bool DownloadFile(string url, string Username, string Password)
        //{
        //    try
        //    {
        //        string basedir = RootDirectory + "/updateFiles";
        //        string ZippedDir = basedir + RelativeDirectory + FileName + ".dng";

        //        if (!System.IO.Directory.Exists(basedir + RelativeDirectory))
        //        {
        //            System.IO.Directory.CreateDirectory(basedir + RelativeDirectory);
        //        }
        //        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(url + "/" + FileName + ".dng"));
        //        request.Credentials = new NetworkCredential(Username, Password);
        //        request.Method = WebRequestMethods.Ftp.DownloadFile;
        //        request.UseBinary = true;
        //        request.UsePassive = false;
        //        request.KeepAlive = true;

        //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        //        string welocme = response.WelcomeMessage;

        //        FileStream stream2 = new FileStream(ZippedDir, FileMode.Create, FileAccess.Write);

        //        Stream responseStream = response.GetResponseStream();
        //        long bufferSize = 1024 * 50;
        //        byte[] buffer = new byte[bufferSize];
        //        int count = responseStream.Read(buffer, 0, buffer.Length);
        //        long nowFileSize = this.FtpFileSize(url, Username, Password);
        //        long totalFileSize = nowFileSize;
        //        long downloaded = 0;
        //        int retry = 0;
        //        while (count > 0L && retry < 3)
        //        {
        //            downloaded += count;
        //            nowFileSize -= count;
        //            stream2.Write(buffer, 0, count);
        //            buffer = new byte[bufferSize];
        //            count = responseStream.Read(buffer, 0, buffer.Length);

        //            if (response.StatusCode == FtpStatusCode.ClosingData)
        //            {
        //                if (downloaded < totalFileSize)
        //                {

        //                    stream2.Close();
        //                    responseStream.Close();
        //                    response.Close();
        //                    retry += 1;

        //                    request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + Username + "@" + url + "/" + FileName));
        //                    request.Credentials = new NetworkCredential(Username, Password);
        //                    request.Method = WebRequestMethods.Ftp.DownloadFile;
        //                    request.UseBinary = true;
        //                    request.UsePassive = false;
        //                    request.KeepAlive = true;
        //                    request.ContentOffset = downloaded;

        //                    response = (FtpWebResponse)request.GetResponse();
        //                    welocme = response.WelcomeMessage;
        //                    Thread.SpinWait(20);

        //                    stream2 = new FileStream(ZippedDir, FileMode.Append, FileAccess.Write);

        //                    responseStream = response.GetResponseStream();
        //                    bufferSize = 1024 * 50;
        //                    buffer = new byte[bufferSize];
        //                    count = responseStream.Read(buffer, 0, buffer.Length);
        //                }
        //            }

        //        }
        //        stream2.Close();
        //        responseStream.Close();
        //        if (retry <= 3)
        //            return true;
        //        return false;
        //    }
        //    catch { return false; }

        //}

        public bool DownloadFile(string url, string Username, string Password)
        {
            try
            {
                url = url.Replace("ftp://protea.dedicated.co.za", "");
                string basedir   = RootDirectory + "/updateFiles";
                string ZippedDir = basedir + RelativeDirectory + FileName + ".dng";

                if (!System.IO.Directory.Exists(basedir + RelativeDirectory))
                {
                    System.IO.Directory.CreateDirectory(basedir + RelativeDirectory);
                }
                Limilabs.FTP.Client.Ftp ftpClient = new Limilabs.FTP.Client.Ftp();
                ftpClient.Connect("protea.dedicated.co.za", 21, false);
                ftpClient.Login("*****@*****.**", "8d8XLgUByXVI");
                ftpClient.Progress += ftpClient_Download_File_Progress;
                this.FileSize       = ftpClient.GetFileSize(url + "/" + FileName + ".dng").ToString();
                if (!System.IO.Directory.Exists(RootDirectory + "/temp"))
                {
                    System.IO.Directory.CreateDirectory(RootDirectory + "/temp");
                }
                ftpClient.Download(url + "/" + FileName + ".dng", RootDirectory + "/temp/" + FileName + ".temp");
                ftpClient.Close(false);
                if (File.Exists(ZippedDir))
                {
                    File.Replace(RootDirectory + "/temp/" + FileName + ".temp", ZippedDir, FileName + ".bak");
                }
                else
                {
                    File.Move(RootDirectory + "/temp/" + FileName + ".temp", ZippedDir);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Deloitte.WriteErrorLog(new DingoException(ex));
                return(false);
            }
        }
示例#2
0
        public bool ExtractFile()
        {
            try
            {
                string basedir      = RootDirectory + "/updateFiles";
                string ZippedDir    = basedir + RelativeDirectory + FileName + ".dng";
                string ExtractedDir = RootDirectory + RelativeDirectory + FileName;
                if (!System.IO.Directory.Exists(RootDirectory + RelativeDirectory))
                {
                    System.IO.Directory.CreateDirectory(RootDirectory + RelativeDirectory);
                }

                if (File.Exists(ZippedDir))
                {
                    using (var writeStream = File.Create(ExtractedDir))
                    {
                        using (var readStream = File.Open(ZippedDir, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (GZipStream zstream = new GZipStream(readStream, CompressionMode.Decompress))
                            {
                                byte[] buffer = new byte[51200];
                                int    count;
                                while ((count = zstream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    writeStream.Write(buffer, 0, count);
                                }
                            }
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                Deloitte.WriteErrorLog(new DingoException(ex));
                return(false);
            }
        }
示例#3
0
文件: Form1.cs 项目: ChAos48/Cashup
 private void CheckFileState(UpdateFile f)
 {
     SetListItem("Comparing Hashes");
     try {
         if (File.Exists(f.Directory + '\\' + f.FileName))
         {
             var onlineHash = GetOnlineHash(f);
             if (f.Hash != onlineHash)
             {
                 CheckZHash(f);
             }
             else
             {
                 SetListItem(f.FileName + " is up to Date");
             }
         }
         else
         {
             CheckZHash(f);
         }
     } catch (Exception ex) {
         Deloitte.WriteErrorLog(new DingoException(ex));
     }
 }