コード例 #1
0
ファイル: FSClient.cs プロジェクト: yaowenhua/NetHadoop
        //下载
        public bool Open(ThriftHadoopFileSystem.Client client, string path, string savePath, long fileLength)
        {
            bool result = false;

            if (client != null)
            {
                ThriftHandle th = client.open(new Pathname()
                {
                    pathname = path
                });

                // 创建文件流
                FileStream fs         = new FileStream(savePath, FileMode.Create, FileAccess.Write);
                long       totalBytes = 0;
                int        readLength = 1024 * 1024;
                try
                {
                    UTF8Encoding utf8 = new UTF8Encoding(false, true);

                    while (true)
                    {
                        int needRead = readLength;
                        if (fileLength - totalBytes < readLength)
                        {
                            needRead = (int)(fileLength - totalBytes);
                        }
                        if (needRead <= 0)
                        {
                            break;
                        }

                        byte[] fileBuffer = client.read(th, totalBytes, readLength);


                        byte[] myfileBuffer = Encoding.Convert(utf8, Encoding.GetEncoding("iso-8859-1"), fileBuffer);


                        totalBytes += readLength;

                        fs.Write(myfileBuffer, 0, myfileBuffer.Length);
                    }
                    result = true;
                }
                catch (Exception ee)
                {
                    throw ee;
                }
                finally
                {
                    fs.Dispose();
                    if (client != null)
                    {
                        client.close(th);
                    }
                }
            }
            return(result);
        }
コード例 #2
0
ファイル: FSClient.cs プロジェクト: yaowenhua/NetHadoop
        //批量下载
        public void MutDownLoad(BackgroundWorker worker, string localRootPath, List <FileStatus> fileList, int fileType)
        {
            //相同操作
            bool sameOp = false;
            //是否覆盖
            bool IsOver = false;

            //准备创建连接
            Thrift.Transport.TBufferedTransport btsport      = null;
            ThriftHadoopFileSystem.Client       thriftClient = Connect(out btsport);

            if (thriftClient != null)//连接成功
            {
                int totalCount   = fileList.Count;
                int currentCount = 0;
                //开始上传
                worker.ReportProgress(0, new ProgressState()
                {
                    ListBoxMsg = "开始下载!", totalCount = totalCount, CurrentCount = 0
                });
                //循环
                foreach (FileStatus myfile in fileList)
                {
                    currentCount++;
                    int pgPresent = (int)((double)currentCount / totalCount * 100);

                    string fileName = Path.GetFileName(myfile.Path);
                    if (myfile.Isdir == false)
                    {
                        string savePath = localRootPath + "/" + fileName;
                        if (fileType == 1 && myfile.FileName != null)
                        {
                            savePath = localRootPath + "/" + myfile.FileName;
                        }
                        if (fileType == 2 && myfile.FileName != null)
                        {
                            savePath = localRootPath + "/" + myfile.FileName + "/" + fileName;
                        }
                        //显示总进度
                        worker.ReportProgress(pgPresent, new ProgressState()
                        {
                            CurrentTitle = fileName, CurrentCount = currentCount
                        });

                        #region 是否存在
                        if (!sameOp)
                        {
                            bool exsitFile = File.Exists(savePath);
                            if (exsitFile)
                            {
                                SureDialog         myDialog = new SureDialog();
                                MyShowDialogResult myDR     = new MyShowDialogResult();
                                myDialog.ShowDialog(fileName, myDR);
                                sameOp = myDR.IsCheck;
                                IsOver = myDR.Result;
                                if (!myDR.Result)
                                {
                                    worker.ReportProgress(pgPresent, new ProgressState()
                                    {
                                        ListBoxMsg = fileName + " 跳过 "
                                    });
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            bool exsitFile = File.Exists(savePath);
                            if (exsitFile)
                            {
                                if (!IsOver)
                                {
                                    worker.ReportProgress(pgPresent, new ProgressState()
                                    {
                                        ListBoxMsg = fileName + "跳过 "
                                    });
                                    continue;
                                }
                            }
                        }
                        #endregion

                        #region 单个下载
                        bool         result = false;
                        ThriftHandle th     = thriftClient.open(new Pathname()
                        {
                            pathname = myfile.Path
                        });
                        // 创建文件流
                        FileStream fs         = new FileStream(savePath, FileMode.Create, FileAccess.Write);
                        long       totalBytes = 0;
                        int        readLength = 1024 * 1024;
                        try
                        {
                            UTF8Encoding utf8 = new UTF8Encoding(false, true);
                            while (true)
                            {
                                int needRead = readLength;
                                if (myfile.Length - totalBytes < readLength)
                                {
                                    needRead = (int)(myfile.Length - totalBytes);
                                }
                                if (needRead <= 0)
                                {
                                    break;
                                }

                                byte[] fileBuffer   = thriftClient.read(th, totalBytes, needRead);
                                byte[] myfileBuffer = Encoding.Convert(utf8, Encoding.GetEncoding("iso-8859-1"), fileBuffer);
                                totalBytes += needRead;
                                fs.Write(myfileBuffer, 0, myfileBuffer.Length);

                                //显示单个上传进度
                                int mypresent = (int)((double)totalBytes / myfile.Length * 100);
                                worker.ReportProgress(pgPresent, new ProgressState()
                                {
                                    CurrentTitle = mypresent + "% " + fileName
                                });
                            }
                            result = true;
                        }
                        catch (Exception ee)
                        {
                            throw ee;
                        }
                        finally
                        {
                            fs.Dispose();
                            if (thriftClient != null)
                            {
                                thriftClient.close(th);
                            }
                        }

                        #endregion

                        string msg = string.Format("{0} 下载{1}", Path.GetFileName(fileName), result ? "成功" : "失败");
                        worker.ReportProgress(pgPresent, new ProgressState()
                        {
                            ListBoxMsg = msg
                        });
                    }
                    else
                    {
                        worker.ReportProgress(pgPresent, new ProgressState()
                        {
                            ListBoxMsg = fileName + "不是文件!"
                        });
                    }
                }
            }

            //释放连接
            if (btsport != null)
            {
                btsport.Close();
            }
        }