namespace RyzStudio.IO
{
    using System;
    using System.IO;
    using ICSharpCode.SharpZipLib.Zip;

    public class SharpZipLib
    {
        public static bool IsZipEncrypted(string filename)
        {
            bool rv = false;

            try
            {
                ZipInputStream zipStream = new ZipInputStream(File.OpenRead(filename));

                ZipEntry zipEntry = null;
                while ((zipEntry = zipStream.GetNextEntry()) != null)
                {
                    if (zipEntry.IsCrypted)
                    {
                        rv = true;
                    }

                    break;
                }

                zipStream.Close();
            }
            catch
            {
                // do nothing
            }

            return rv;
        }

        public static bool TestZipEncrypted(string filename, string password)
        {
            bool rv = false;

            int size = 2;
            byte[] buffer = new byte[size];
            int bufferSize = 0;

            ZipEntry readEntry = null;
            ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename));
            readStream.Password = password;

            while (true)
            {
                readEntry = readStream.GetNextEntry();
                if (readEntry == null)
                {
                    break;
                }

                if (string.IsNullOrWhiteSpace(readEntry.Name))
                {
                    continue;
                }

                if (!readEntry.IsFile)
                {
                    continue;
                }

                if (readEntry.Size <= 0)
                {
                    continue;
                }

                try
                {
                    bufferSize = readStream.Read(buffer, 0, buffer.Length);
                    rv = true;
                }
                catch
                {
                    break;
                }

                break;
            }

            return rv;
        }

        public static void AddFile(ZipOutputStream zipStream, string filename, string prefixPath = null)
        {
            byte[] buffer = new byte[4096];

            string f1 = "";
            if (prefixPath != null)
            {
                f1 = Path.GetDirectoryName(filename).TrimEnd('\\') + "\\";
                f1 = f1.Replace(prefixPath, "").TrimEnd('\\') + "\\";
                f1 = f1 + Path.GetFileName(filename);
                f1 = f1.TrimStart('\\');
            }

            ZipEntry zipEntry = new ZipEntry(f1);
            zipEntry.DateTime = DateTime.Now;
            zipStream.PutNextEntry(zipEntry);

            FileStream fileStream = File.OpenRead(filename);
            int sourceBytes;
            do
            {
                sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
                zipStream.Write(buffer, 0, sourceBytes);
            }
            while (sourceBytes > 0);
        }

        public static void AddFolder(ZipOutputStream zipStream, string folderPath, string prefixPath = null)
        {
            foreach (string fn in Directory.GetFiles(folderPath, "*.*", System.IO.SearchOption.AllDirectories))
            {
                AddFile(zipStream, fn, prefixPath);
            }
        }
    }
}