private void SetupScormEngine(bool update = false)
        {
            string scormPackagePath = FilesDir.AbsolutePath + "/ScormPackage";
            string scormZipPath     = FilesDir.AbsolutePath + "/ScormPackage.zip";

            if (update)
            {
                IpcCommon.CommonUtils.GetInstance().DeleteContent(scormPackagePath);
            }
            try
            {
                if (!Directory.Exists(scormPackagePath))
                {
                    using (BinaryReader reader = new BinaryReader(Assets.Open("ScormPackage.zip")))
                    {
                        using (BinaryWriter writer = new BinaryWriter(new FileStream(scormZipPath, FileMode.Create)))
                        {
                            byte[] buffer = new byte[2048];
                            int    length = 0;
                            while ((length = reader.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                writer.Write(buffer, 0, length);
                            }
                        }
                    }

                    UnzipHandler unzipHandler = new UnzipHandler(FilesDir.AbsolutePath, scormZipPath, null);
                    unzipHandler.UnzipContent();
                    //delete the .zip file after extraction.
                    Console.WriteLine("");
                }
            }
            catch (Exception e)
            {
            }
        }
Esempio n. 2
0
        public bool UnZipFile(string zipFilePath, string unZipDir, UnzipHandler unzipHandler, out string errorMessage)
        {
            errorMessage = string.Empty;
            if (zipFilePath == string.Empty || !File.Exists(zipFilePath))
            {
                errorMessage = "Zip File Not Found";
                return(false);
            }

            if (unZipDir == string.Empty)
            {
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            }

            if (!unZipDir.EndsWith("\\"))
            {
                unZipDir += "\\";
            }

            if (!Directory.Exists(unZipDir))
            {
                Directory.CreateDirectory(unZipDir);
            }

            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string    directoryName = Path.GetDirectoryName(theEntry.Name);
                        string    fileName      = Path.GetFileName(theEntry.Name);
                        string    relFileName   = directoryName + "\\" + fileName;
                        UnzipCode code          = unzipHandler(relFileName, null);
                        if (directoryName.Length > 0)
                        {
                            string pathName = unZipDir + directoryName;
                            if (!Directory.Exists(pathName))
                            {
                                Directory.CreateDirectory(pathName);
                            }
                        }

                        if (!directoryName.EndsWith("\\"))
                        {
                            directoryName += "\\";
                        }
                        if (fileName != String.Empty)
                        {
                            MemoryStream ms = new MemoryStream();
                            while (true)
                            {
                                int r = s.Read(this.buffer, 0, this.buffer.Length);
                                if (r > 0)
                                {
                                    ms.Write(this.buffer, 0, r);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (code == UnzipCode.Compare)
                            {
                                ms.Seek(0, SeekOrigin.Begin);
                                unzipHandler(relFileName, ms);
                            }
                            else if (code == UnzipCode.Ignore)
                            {
                                continue;
                            }

                            string destFileName = unZipDir + theEntry.Name;
                            using (FileStream streamWriter = File.Create(destFileName))
                            {
                                ms.Seek(0, SeekOrigin.Begin);
                                while (true)
                                {
                                    int r = ms.Read(this.buffer, 0, this.buffer.Length);
                                    if (r > 0)
                                    {
                                        streamWriter.Write(this.buffer, 0, r);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                UpdateLog.Instance().AddName(theEntry.Name);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
File: Zip.cs Progetto: oisy/scada
        public bool UnZipFile(string zipFilePath, string unZipDir, UnzipHandler unzipHandler, out string errorMessage)
        {
            errorMessage = string.Empty;
            if (zipFilePath == string.Empty || !File.Exists(zipFilePath))
            {
                errorMessage = "Zip File Not Found";
                return false;
            }

            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));

            if (!unZipDir.EndsWith("\\"))
                unZipDir += "\\";

            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        string relFileName = directoryName + "\\" + fileName;
                        UnzipCode code = unzipHandler(relFileName, null);
                        if (directoryName.Length > 0)
                        {
                            string pathName = unZipDir + directoryName;
                            if (!Directory.Exists(pathName))
                            {
                                Directory.CreateDirectory(pathName);
                            }
                        }

                        if (!directoryName.EndsWith("\\"))
                            directoryName += "\\";
                        if (fileName != String.Empty)
                        {
                            MemoryStream ms = new MemoryStream();
                            while (true)
                            {
                                int r = s.Read(this.buffer, 0, this.buffer.Length);
                                if (r > 0)
                                {
                                    ms.Write(this.buffer, 0, r);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (code == UnzipCode.Compare)
                            {
                                ms.Seek(0, SeekOrigin.Begin);
                                unzipHandler(relFileName, ms);
                            }
                            else if (code == UnzipCode.Ignore)
                            {
                                continue;
                            }

                            string destFileName = unZipDir + theEntry.Name;
                            using (FileStream streamWriter = File.Create(destFileName))
                            {
                                ms.Seek(0, SeekOrigin.Begin);
                                while (true)
                                {
                                    int r = ms.Read(this.buffer, 0, this.buffer.Length);
                                    if (r > 0)
                                    {
                                        streamWriter.Write(this.buffer, 0, r);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                UpdateLog.Instance().AddName(theEntry.Name);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return false;
            }
            return true;
        }