using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace Stone.Class
{
    public static class DBFilesCompress
    {
        public static string XmlDataFileNameInformation = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Information.xml";
        public static string XmlSchemaFileNameInformation = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Information.xsd";

        public static string XmlDataFileNameProducts = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xml";
        public static string XmlSchemaFileNameProducts = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xsd";

        public static bool DBCompress(string StrPathDestination)
        {

            try
            {
                FileStream outputFileStream = new FileStream(StrPathDestination, FileMode.Create);
                ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(outputFileStream);
                ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = null;
                Stream inputStream;

                //copy file PropertiesFactor.xml
                inputStream = new FileStream(Program.FC.XmlDataFileNamePropertiesFactor, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(Program.FC.XmlDataFileNamePropertiesFactor));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);

                //copy file PropertiesFactor.xsd

                inputStream = new FileStream(Program.FC.XmlSchemaFileNamePropertiesFactor, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(Program.FC.XmlSchemaFileNamePropertiesFactor));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);



                //copy file DetailFactor.xml

                inputStream = new FileStream(Program.FC.XmlDataFileNameDetailFactor, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(Program.FC.XmlDataFileNameDetailFactor));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);


                //copy file DetailFactor.xsd

                inputStream = new FileStream(Program.FC.XmlSchemaFileNameDetailFactor, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(Program.FC.XmlSchemaFileNameDetailFactor));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);


                //copy file Information.xml

                inputStream = new FileStream(XmlDataFileNameInformation, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(XmlDataFileNameInformation));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);


                //copy file Information.xsd

                inputStream = new FileStream(XmlSchemaFileNameInformation, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(XmlSchemaFileNameInformation));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);


                //copy file Products.xml

                inputStream = new FileStream(XmlDataFileNameProducts, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(XmlDataFileNameProducts));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);

                //copy file Products.xsd


                inputStream = new FileStream(XmlSchemaFileNameProducts, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(XmlSchemaFileNameProducts));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);




                //copy file Account.xml

                inputStream = new FileStream(Program.ACU.XmlDataFileName, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(Program.ACU.XmlDataFileName));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);

                //copy file Account.xsd


                inputStream = new FileStream(Program.ACU.XmlSchemaFileName, FileMode.Open);
                zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(Program.ACU.XmlSchemaFileName));

                zipEntry.IsVisible = true;
                zipEntry.IsCrypted = false;

                zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
                zipStream.PutNextEntry(zipEntry);

                CopyStream(inputStream, zipStream);










                inputStream.Close();
                zipStream.CloseEntry();

                zipStream.Finish();
                zipStream.Close();
                return true;
            }
            catch (Exception ex)
            {
               return false;
            }


        }



        private static void CopyStream(Stream source, Stream destination)
        {
            byte[] buffer = new byte[4096];
            int countBytesRead;
            while ((countBytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                destination.Write(buffer, 0, countBytesRead);
            }
        }


        public static bool DECompressDataBase(string StrPathFileZip)
        {

            ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry;
            ICSharpCode.SharpZipLib.Zip.ZipFile zipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(StrPathFileZip);

            try
            {

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xml"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xml");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xsd"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xsd");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xml"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xml");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xsd"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xsd");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xml"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xml");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xsd"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xsd");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Information.xml"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Information.xml");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Information.xsd"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Information.xsd");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Account.xml"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Account.xml");

                if (System.IO.File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Account.xsd"))
                    System.IO.File.Delete(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Account.xsd");
               
                zipEntry = zipFile[0];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[1];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[2];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[3];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }



                zipEntry = zipFile[4];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[5];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[6];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[7];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }


                zipEntry = zipFile[8];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }

                zipEntry = zipFile[9];

                if (zipEntry != null)
                {
                    Stream inputStream = zipFile.GetInputStream(zipEntry);
                    FileStream fileStream = new FileStream(
                        Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB", zipEntry.Name),
                        FileMode.Create);
                    CopyStream(inputStream, fileStream);
                    fileStream.Close();
                    inputStream.Close();
                }












                zipFile.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;

            }

        }

    }
}
