Esempio n. 1
0
        static private string RootZipEntry(string FilePath, string FullZipEntry,
                                           ref string RootZipPath, ref string FullRootPath)
        {
            string zipEntry = string.Empty;

            if (FullZipEntry == null)
            {
                FullZipEntry = ZipArchiveMOD.ZipEntry(FilePath, true);
            }
            if ((FilePath != null) && (FilePath != string.Empty))
            {
                zipEntry = ZipArchiveMOD.ZipEntry(FilePath, true);
                if ((zipEntry != FullZipEntry) &&
                    (!zipEntry.EndsWith(@"\")))
                {
                    zipEntry += @"\";
                }
                DirectoryInfo di = new DirectoryInfo(FilePath);
                if ((RootZipPath.IndexOf(zipEntry) == -1) &&
                    (di.Parent != null))
                {
                    FullRootPath = di.Parent.FullName;
                    zipEntry     = ZipArchiveMOD.RootZipEntry(FullRootPath,
                                                              FullZipEntry, ref RootZipPath, ref FullRootPath);
                }
                else
                {
                    RootZipPath = zipEntry;
                    zipEntry    = FullZipEntry.Replace(zipEntry, string.Empty);
                }
            }
            return(zipEntry);
        }
Esempio n. 2
0
        public bool DownloadTheLastestVersion()
        {
            bool result = true;

            UpdateStatusProgressBar(0, "Chuẩn bị dữ liệu...");

            //Step 0: Prepare data
            string sPath = Path.GetDirectoryName(Application.ExecutablePath);

            UpdateStatusProgressBar(10, "Đang lấy thông tin phiên bản mới...");
            //Step 1: Get the lastest version
            VersionOBJ verObj = VersionCTL.GetLastestVersion();

            if (verObj.VersionID == string.Empty)
            {
                MessageBox.Show("Lỗi! Không thể cập nhật phiên bản mới!");
                return(false);
            }

            string szipFilePath = sPath + "\\CRM_ver_" + verObj.VersionID + ".zip";

            UpdateStatusProgressBar(45, "Đang tải tập tin...");
            //Step 2: Download zip file from server and save in the Path
            if (SegmentDataCTL.DownloadFileFromServer(verObj.VersionID, szipFilePath) == false)
            {
                MessageBox.Show("Lỗi! Không thể tải tập tin!");
                return(false);
            }

            UpdateStatusProgressBar(20, "Đang cập nhật phiên bản mới...");
            //Step 3: Extract zip file
            try
            {
                string sOutputPath = sPath;
                ZipArchiveMOD.UnzipFile(szipFilePath, sOutputPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi! Không thể giải nVN tập tin! Chi tiết: " + ex.Message);
                return(false);
            }

            UpdateStatusProgressBar(0, "Hoàn tất");
            //Step 4: Write a new version to file
            string sfileVerPath = sPath + "\\version.dat";

            VersionCTL.WriteNewVersion(verObj, sfileVerPath);
            File.Delete(szipFilePath);
            MessageBox.Show("Cập nhật phiên bản mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return(true);
        }
        public void DownloadTheLastestVersion()
        {
            UpdateStatusProgressBar(0, "Preparing data...");

            //Step 0: Prepare data
            string sPath = Path.GetDirectoryName(Application.ExecutablePath);

            UpdateStatusProgressBar(10, "Getting lastest version...");
            //Step 1: Get the lastest version
            VersionOBJ verObj = VersionCTL.GetLastestVersion();

            if (verObj.VersionID == string.Empty)
            {
                MessageBox.Show("Error! Can't get new version!");
                return;
            }
            //string szipFilePath = sPath + "\\" + verObj.FileName;
            string szipFilePath = sPath + "\\CRM_Download.zip";

            UpdateStatusProgressBar(45, "Downloading file...");
            //Step 2: Download zip file from server and save in the Path
            if (SegmentDataCTL.DownloadFileFromServer(verObj.VersionID, szipFilePath) == false)
            {
                MessageBox.Show("Error! Can't download upload file!");
                return;
            }

            UpdateStatusProgressBar(20, "Updating file...");
            //Step 3: Extract zip file
            try
            {
                string sOutputPath = sPath + "\\tmp1";
                ZipArchiveMOD.UnzipFile(szipFilePath, sOutputPath);
            }
            catch
            {
                MessageBox.Show("Error! Can't extract zip file!");
                return;
            }

            UpdateStatusProgressBar(0, "Finished");
            MessageBox.Show("Download file successfully!");
            //Step 4: Copy and overwrite (maybe) to folder QuanLyDiem

            btn_Upload.Enabled   = true;
            btn_Download.Enabled = true;
        }
Esempio n. 4
0
        //public static void CreateArchive(string ArchiveFilePath, string SolutionFilePath, string[] FilePaths, bool RelativePath)
        public static void CreateArchive(string ArchiveFilePath, string SrcFolderPath, string[] FilePaths, bool RelativePath)
        {
            const int       blockSize   = 16384;
            FileStream      fs          = new FileStream(ArchiveFilePath, FileMode.Create);
            ZipOutputStream zipStream   = new ZipOutputStream(fs);
            string          zipRootPath = ZipArchiveMOD.RootPathFromFiles(SrcFolderPath, FilePaths);

            foreach (string filePath in FilePaths)
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    FileStream streamToZip = new FileStream(filePath,
                                                            FileMode.Open, FileAccess.Read);
                    string zipPath = string.Empty;
                    if (RelativePath)
                    {
                        string fullRootPath = filePath;
                        zipPath = ZipArchiveMOD.RootZipEntry(filePath,
                                                             null, ref zipRootPath, ref fullRootPath);

                        //NBPhuc 30/06/2013 (Khong co thu muc)
                        zipPath = Path.GetFileName(zipPath);
                    }
                    else
                    {
                        zipPath = ZipArchiveMOD.ZipEntry(filePath, true);
                    }
                    ZipEntry zipEntry = new ZipEntry(zipPath);
                    zipStream.PutNextEntry(zipEntry);
                    byte[] buffer = new byte[blockSize];
                    int    size   = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, size);
                    while (size < streamToZip.Length)
                    {
                        int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                    }
                    streamToZip.Close();
                }
            }
            zipStream.Finish();
            zipStream.Close();
            fs.Close();
        }
Esempio n. 5
0
        static private string RootPathFromFiles(string InitRootPath,
                                                string[] FilePaths)
        {
            string        path = string.Empty;
            DirectoryInfo di   = new DirectoryInfo(InitRootPath);

            if (di.Exists)
            {
                path = ZipArchiveMOD.ZipEntry(InitRootPath, true);
            }
            else
            {
                FileInfo fi = new FileInfo(InitRootPath);
                if (fi.Exists)
                {
                    path = ZipArchiveMOD.ZipEntry(InitRootPath, false);
                }
            }
            foreach (string filePath in FilePaths)
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    string initPath = ZipArchiveMOD.ZipEntry(
                        InitRootPath, false);
                    string zipRootPath  = initPath;
                    string fullRootPath = InitRootPath;
                    string zipPath      = ZipArchiveMOD.RootZipEntry(
                        filePath, null, ref zipRootPath, ref fullRootPath);
                    if ((initPath.IndexOf(zipRootPath) == 0) &&
                        (zipRootPath.Length < initPath.Length))
                    {
                        return(ZipArchiveMOD.RootPathFromFiles(fullRootPath,
                                                               FilePaths));
                    }
                }
            }
            return(path);
        }
        public void UploadNewVersion()
        {
            DateTime startDT = DateTime.Now;

            try
            {
                UpdateStatusProgressBar(0, "Preparing data...");

                //Step 0: Prepare data
                string sPath        = Path.GetDirectoryName(Application.ExecutablePath);
                string szipFilePath = sPath + "\\" + txt_FileName.Text;

                UpdateStatusProgressBar(0, "Creating new zip file...");

                //Step 1: Create new zip file (includes list of update files)
                try
                {
                    string sUpdateFolder = sPath;
                    ZipArchiveMOD.CreateArchive(szipFilePath, sUpdateFolder, this.lstUploadFiles, true);
                }
                catch (Exception ex1)
                {
                    MessageBox.Show("Error! Create zip file: " + ex1.Message);
                    btn_Upload.Enabled   = true;
                    btn_Download.Enabled = true;

                    return;
                }

                UpdateStatusProgressBar(45, "Inserting version...");

                //Step 2: Insert version
                VersionOBJ verObj = new VersionOBJ();
                verObj.VersionID   = txt_VersionID.Text;
                verObj.VersionName = txt_VersionName.Text;
                verObj.FileName    = txt_FileName.Text;
                //FileInfo fi = new FileInfo(szipFilePath);
                //verObj.FileSize = fi.Length;
                verObj.FileSize    = (new FileInfo(szipFilePath)).Length;
                verObj.VersionType = "TH-CRM";
                verObj.Notes       = txt_Notes.Text;

                UpdateStatusProgressBar(0, "Inserting version...");

                if (VersionCTL.InsertNewVersion(verObj) == false)
                {
                    MessageBox.Show("Error! Insert Version");
                    btn_Upload.Enabled   = true;
                    btn_Download.Enabled = true;
                    return;
                }

                UpdateStatusProgressBar(0, "Uploading file to server...");

                //Step 3: Upload file to server
                if (SegmentDataCTL.UploadFileToServer(szipFilePath, verObj.VersionID, verObj.Notes))
                {
                    DateTime endDT = DateTime.Now;
                    //DateTime delta = endDT - startDT;
                    long totalSecond = 0;
                    if (endDT.Second > startDT.Second)
                    {
                        totalSecond = (endDT.Hour - startDT.Hour) * 60 * 60 + (endDT.Minute - startDT.Minute) * 60 + (endDT.Second - startDT.Second);
                    }
                    else
                    {
                        totalSecond = (endDT.Hour - startDT.Hour) * 60 * 60 + (endDT.Minute - startDT.Minute - 1) * 60 + (endDT.Second + 60 - startDT.Second);
                    }

                    UpdateStatusProgressBar(25, "Finished");

                    MessageBox.Show("Files uploaded successfully! Total second: " + totalSecond.ToString());
                }
                else
                {
                    MessageBox.Show("Error! Upload file!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            btn_Upload.Enabled   = true;
            btn_Download.Enabled = true;

            //thread.Abort();
        }