Beispiel #1
0
 public void ExtractAll(string where, SelfAwareEnumValue <ZipRestoreFilePathsModeEnum> restorePaths = null)
 {
     CheckIfOpened();
     _zip.FlattenFoldersOnExtract = FlattenPathsOnExtraction(restorePaths);
     _zip.ExtractExistingFile     = ExtractExistingFileAction.OverwriteSilently;
     _zip.ExtractAll(where);
 }
Beispiel #2
0
        private EncryptionAlgorithm MakeZipEncryption(SelfAwareEnumValue <ZipEncryptionMethodEnum> encryptionMethod)
        {
            if (encryptionMethod == null)
            {
                return(EncryptionAlgorithm.None);
            }

            var enumOwner = (ZipEncryptionMethodEnum)encryptionMethod.Owner;

            if (encryptionMethod == enumOwner.Zip20)
            {
                return(EncryptionAlgorithm.PkzipWeak);
            }
            if (encryptionMethod == enumOwner.Aes128)
            {
                return(EncryptionAlgorithm.WinZipAes128);
            }
            if (encryptionMethod == enumOwner.Aes192)
            {
                return(EncryptionAlgorithm.Unsupported);
            }
            if (encryptionMethod == enumOwner.Aes256)
            {
                return(EncryptionAlgorithm.WinZipAes256);
            }

            throw RuntimeException.InvalidArgumentValue();
        }
Beispiel #3
0
        public void Extract(ZipFileEntryContext entry, string destination, SelfAwareEnumValue <ZipRestoreFilePathsModeEnum> restorePaths = null, string password = null)
        {
            CheckIfOpened();
            var realEntry = entry.GetZipEntry();

            _zip.FlattenFoldersOnExtract = FlattenPathsOnExtraction(restorePaths);
            realEntry.Password           = password;
            realEntry.Extract(destination);
        }
Beispiel #4
0
        public string DecodeString(string encodedString, SelfAwareEnumValue <StringEncodingMethodEnum> codeType, IValue encoding = null)
        {
            if (encoding != null)
            {
                throw new NotSupportedException("Явное указание кодировки в данной версии не поддерживается (только utf-8 согласно RFC 3986)");
            }

            return(Uri.UnescapeDataString(encodedString));
        }
Beispiel #5
0
 private static bool GetRecursiveFlag(SelfAwareEnumValue <ZIPSubDirProcessingModeEnum> recurseSubdirectories)
 {
     if (recurseSubdirectories == null)
     {
         return(false);
     }
     else
     {
         return(recurseSubdirectories == ((ZIPSubDirProcessingModeEnum)recurseSubdirectories.Owner).Recurse);
     }
 }
Beispiel #6
0
        private static bool FlattenPathsOnExtraction(SelfAwareEnumValue <ZipRestoreFilePathsModeEnum> restorePaths)
        {
            bool flattenFlag = false;

            if (restorePaths != null)
            {
                var zipEnum = (ZipRestoreFilePathsModeEnum)restorePaths.Owner;
                flattenFlag = restorePaths == zipEnum.DoNotRestore;
            }

            return(flattenFlag);
        }
Beispiel #7
0
        public void SortByValue(SelfAwareEnumValue <SortDirectionEnum> direction)
        {
            var enumInstance = GlobalsManager.GetEnum <SortDirectionEnum>();

            if (direction == enumInstance.Asc)
            {
                _items.Sort((x, y) => SafeCompare(x.Value, y.Value));
            }
            else
            {
                _items.Sort((x, y) => SafeCompare(y.Value, x.Value));
            }
        }
Beispiel #8
0
        public void SortByPresentation(SelfAwareEnumValue <SortDirectionEnum> direction)
        {
            var enumInstance = GlobalsManager.GetEnum <SortDirectionEnum>();

            if (direction == enumInstance.Asc)
            {
                _items.Sort((x, y) => x.Presentation.CompareTo(y.Presentation));
            }
            else
            {
                _items.Sort((x, y) => y.Presentation.CompareTo(x.Presentation));
            }
        }
Beispiel #9
0
        public void Extract(ZipFileEntryContext entry, string destination, SelfAwareEnumValue <ZipRestoreFilePathsModeEnum> restorePaths = null, string password = null)
        {
            CheckIfOpened();
            var realEntry = entry.GetZipEntry();

            _zip.FlattenFoldersOnExtract = FlattenPathsOnExtraction(restorePaths);
            realEntry.Password           = password;

            using (FileStream streamToExtract = new FileStream(Path.Combine(destination, entry.Name), FileMode.Create))
            {
                realEntry.Extract(streamToExtract);
            }
        }
Beispiel #10
0
        private void AddDirectory(string dir, SearchOption searchOption, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            string allFilesMask;

            if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == PlatformID.MacOSX)
            {
                allFilesMask = "*";
            }
            else
            {
                allFilesMask = "*.*";
            }

            var filesToAdd = Directory.EnumerateFiles(dir, allFilesMask, searchOption);

            AddEnumeratedFiles(filesToAdd, GetPathForParentFolder(dir), storePathMode);
        }
Beispiel #11
0
        private void AddFilesByMask(string file, SearchOption searchOption, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            // надо разделить на каталог и маску
            var    pathEnd = file.LastIndexOfAny(new[] { '\\', '/' });
            string path;
            string mask;
            IEnumerable <string> filesToAdd;

            if (pathEnd > 1)
            {
                path = file.Substring(0, pathEnd);
                var maskLen = file.Length - pathEnd - 1;
                if (maskLen > 0)
                {
                    mask = file.Substring(pathEnd + 1, maskLen);
                }
                else
                {
                    // маска была не в конце пути
                    // 1С такое откидывает
                    return;
                }

                // несуществующие пути или пути к файлам, вместо папок 1С откидывает
                if (!Directory.Exists(path))
                {
                    return;
                }
            }
            else if (pathEnd == 0)
            {
                path = "";
                mask = file.Substring(1);
            }
            else
            {
                path = "";
                mask = file;
            }

            filesToAdd = Directory.EnumerateFiles(path, mask, searchOption);
            var relativePath = Path.GetFullPath(path);

            AddEnumeratedFiles(filesToAdd, relativePath, storePathMode);
        }
Beispiel #12
0
        private void AddDirectory(string file, System.IO.SearchOption searchOption, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            var    path = Path.IsPathRooted(file) ? Path.GetDirectoryName(file) : Path.GetFullPath(file);
            string allFilesMask;

            if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == PlatformID.MacOSX)
            {
                allFilesMask = "*";
            }
            else
            {
                allFilesMask = "*.*";
            }

            var filesToAdd = System.IO.Directory.EnumerateFiles(file, allFilesMask, searchOption);

            AddEnumeratedFiles(filesToAdd, path, storePathMode);
        }
Beispiel #13
0
        private void AddDirectory(string file, System.IO.SearchOption searchOption, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            IEnumerable <string> filesToAdd;
            string path = file;
            string allFilesMask;

            if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == PlatformID.MacOSX)
            {
                allFilesMask = "*";
            }
            else
            {
                allFilesMask = "*.*";
            }

            filesToAdd = System.IO.Directory.EnumerateFiles(file, allFilesMask, searchOption);
            AddEnumeratedFiles(filesToAdd, path, storePathMode);
        }
Beispiel #14
0
 public void Open(
     string filename,
     string password = null,
     string comment  = null,
     SelfAwareEnumValue <ZipCompressionMethodEnum> compressionMethod = null,
     SelfAwareEnumValue <ZipCompressionLevelEnum> compressionLevel   = null,
     SelfAwareEnumValue <ZipEncryptionMethodEnum> encryptionMethod   = null)
 {
     _filename = filename;
     _zip      = new ZipFile();
     _zip.AlternateEncoding      = Encoding.UTF8;
     _zip.AlternateEncodingUsage = ZipOption.Always;
     _zip.Password          = password;
     _zip.Comment           = comment;
     _zip.CompressionMethod = MakeZipCompressionMethod(compressionMethod);
     _zip.CompressionLevel  = MakeZipCompressionLevel(compressionLevel);
     // Zlib падает с NullReferenceException, если задать шифрование
     //_zip.Encryption = MakeZipEncryption(encryptionMethod);
 }
Beispiel #15
0
 public void Open(
     string filename,
     string password = null,
     string comment  = null,
     SelfAwareEnumValue <ZipCompressionMethodEnum> compressionMethod = null,
     SelfAwareEnumValue <ZipCompressionLevelEnum> compressionLevel   = null,
     SelfAwareEnumValue <ZipEncryptionMethodEnum> encryptionMethod   = null)
 {
     _filename = filename;
     _zip      = new ZipFile();
     _zip.AlternateEncoding      = Encoding.GetEncoding(866); // f**k non-russian encodings on non-ascii files
     _zip.AlternateEncodingUsage = ZipOption.Always;
     _zip.Password           = password;
     _zip.Comment            = comment;
     _zip.CompressionMethod  = MakeZipCompressionMethod(compressionMethod);
     _zip.CompressionLevel   = MakeZipCompressionLevel(compressionLevel);
     _zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
     // Zlib падает с NullReferenceException, если задать шифрование
     //_zip.Encryption = MakeZipEncryption(encryptionMethod);
 }
Beispiel #16
0
        private CompressionMethod MakeZipCompressionMethod(SelfAwareEnumValue <ZipCompressionMethodEnum> compressionMethod)
        {
            if (compressionMethod == null)
            {
                return(CompressionMethod.Deflate);
            }

            var owner = (ZipCompressionMethodEnum)compressionMethod.Owner;

            if (compressionMethod == owner.Deflate)
            {
                return(CompressionMethod.Deflate);
            }
            if (compressionMethod == owner.Copy)
            {
                return(CompressionMethod.None);
            }

            throw RuntimeException.InvalidArgumentValue();
        }
Beispiel #17
0
 public void Open(
     string filename,
     string password = null,
     string comment  = null,
     SelfAwareEnumValue <ZipCompressionMethodEnum> compressionMethod = null,
     SelfAwareEnumValue <ZipCompressionLevelEnum> compressionLevel   = null,
     SelfAwareEnumValue <ZipEncryptionMethodEnum> encryptionMethod   = null,
     FileNamesEncodingInZipFile encoding = FileNamesEncodingInZipFile.Auto)
 {
     ZipFile.DefaultEncoding = Encoding.GetEncoding(866); // f**k non-russian encodings on non-ascii files
     _filename = filename;
     _zip      = new ZipFile();
     _zip.AlternateEncoding      = Encoding.UTF8;
     _zip.AlternateEncodingUsage = ChooseEncodingMode(encoding);
     _zip.Password           = password;
     _zip.Comment            = comment;
     _zip.CompressionMethod  = MakeZipCompressionMethod(compressionMethod);
     _zip.CompressionLevel   = MakeZipCompressionLevel(compressionLevel);
     _zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
     _zip.Encryption         = MakeZipEncryption(encryptionMethod);
 }
Beispiel #18
0
        public void Add(string file, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode = null, SelfAwareEnumValue <ZIPSubDirProcessingModeEnum> recurseSubdirectories = null)
        {
            CheckIfOpened();

            var pathIsMasked = file.IndexOfAny(new[] { '*', '?' }) >= 0;

            var recursiveFlag = GetRecursiveFlag(recurseSubdirectories);
            var searchOption  = recursiveFlag ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            if (pathIsMasked)
            {
                AddFilesByMask(file, searchOption, storePathMode);
            }
            else if (Directory.Exists(file))
            {
                AddDirectory(file, searchOption, storePathMode);
            }
            else if (File.Exists(file))
            {
                AddSingleFile(file, storePathMode);
            }
        }
Beispiel #19
0
        private void AddSingleFile(string file, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            var storeModeEnum = GlobalsManager.GetEnum <ZipStorePathModeEnum>();

            if (storePathMode == null)
            {
                storePathMode = (SelfAwareEnumValue <ZipStorePathModeEnum>)storeModeEnum.StoreRelativePath;
            }

            string pathInArchive;

            if (storePathMode == storeModeEnum.StoreFullPath)
            {
                pathInArchive = null;
            }
            else
            {
                pathInArchive = "";
            }


            _zip.AddFile(file, pathInArchive);
        }
Beispiel #20
0
        public string EncodeString(string sourceString, SelfAwareEnumValue <StringEncodingMethodEnum> codeType, IValue encoding = null)
        {
            var      encMethod = GlobalsManager.GetEnum <StringEncodingMethodEnum>();
            Encoding enc;

            if (encoding != null)
            {
                enc = TextEncodingEnum.GetEncoding(encoding);
            }
            else
            {
                enc = Encoding.UTF8;
            }

            if (codeType == encMethod.URLEncoding)
            {
                return(EncodeStringImpl(sourceString, enc, false));
            }
            else
            {
                return(EncodeStringImpl(sourceString, enc, true));
            }
        }
Beispiel #21
0
        private CompressionLevel MakeZipCompressionLevel(SelfAwareEnumValue <ZipCompressionLevelEnum> compressionLevel)
        {
            if (compressionLevel == null)
            {
                return(CompressionLevel.Default);
            }

            var owner = (ZipCompressionLevelEnum)compressionLevel.Owner;

            if (compressionLevel == owner.Minimal)
            {
                return(CompressionLevel.BestSpeed);
            }
            if (compressionLevel == owner.Optimal)
            {
                return(CompressionLevel.Default);
            }
            if (compressionLevel == owner.Maximal)
            {
                return(CompressionLevel.BestCompression);
            }

            throw RuntimeException.InvalidArgumentValue();
        }
Beispiel #22
0
        private void AddSingleFile(string file, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            var storeModeEnum = GlobalsManager.GetEnum <ZipStorePathModeEnum>();

            if (storePathMode == null)
            {
                storePathMode = (SelfAwareEnumValue <ZipStorePathModeEnum>)storeModeEnum.StoreRelativePath;
            }

            var currDir = Directory.GetCurrentDirectory();

            string pathInArchive;

            if (storePathMode == storeModeEnum.StoreFullPath)
            {
                pathInArchive = null;
            }
            else if (storePathMode == storeModeEnum.StoreRelativePath)
            {
                var relativePath = GetRelativePath(file, currDir);
                if (relativePath == "")
                {
                    pathInArchive = ".";
                }
                else
                {
                    pathInArchive = Path.GetDirectoryName(relativePath);
                }
            }
            else
            {
                pathInArchive = "";
            }

            _zip.AddFile(file, pathInArchive);
        }
Beispiel #23
0
        public int StrFind(string haystack, string needle, SelfAwareEnumValue <SearchDirectionEnum> direction = null, int startPos = 0, int occurance = 0)
        {
            int len = haystack.Length;

            if (len == 0 || needle.Length == 0)
            {
                return(0);
            }

            if (direction == null)
            {
                direction = GlobalsManager.GetEnum <SearchDirectionEnum>().FromBegin as SelfAwareEnumValue <SearchDirectionEnum>;
            }

            bool fromBegin = direction == GlobalsManager.GetEnum <SearchDirectionEnum>().FromBegin;

            if (startPos == 0)
            {
                startPos = fromBegin ? 1 : len;
            }

            if (startPos < 1 || startPos > len)
            {
                throw RuntimeException.InvalidArgumentValue();
            }

            if (occurance == 0)
            {
                occurance = 1;
            }

            int startIndex = startPos - 1;
            int foundTimes = 0;
            int index      = len + 1;

            if (fromBegin)
            {
                while (foundTimes < occurance && index >= 0)
                {
                    index = haystack.IndexOf(needle, startIndex);
                    if (index >= 0)
                    {
                        startIndex = index + 1;
                        foundTimes++;
                    }
                    if (startIndex >= len)
                    {
                        break;
                    }
                }
            }
            else
            {
                while (foundTimes < occurance && index >= 0)
                {
                    index = haystack.LastIndexOf(needle, startIndex);
                    if (index >= 0)
                    {
                        startIndex = index - 1;
                        foundTimes++;
                    }
                    if (startIndex < 0)
                    {
                        break;
                    }
                }
            }

            if (foundTimes == occurance)
            {
                return(index + 1);
            }
            else
            {
                return(0);
            }
        }
Beispiel #24
0
        private void AddEnumeratedFiles(IEnumerable <string> filesToAdd, string relativePath, SelfAwareEnumValue <ZipStorePathModeEnum> storePathMode)
        {
            var storeModeEnum = GlobalsManager.GetEnum <ZipStorePathModeEnum>();

            if (storePathMode == null)
            {
                storePathMode = (SelfAwareEnumValue <ZipStorePathModeEnum>)storeModeEnum.DontStorePath;
            }

            foreach (var item in filesToAdd)
            {
                string pathInArchive;
                if (storePathMode == storeModeEnum.StoreRelativePath)
                {
                    pathInArchive = Path.GetDirectoryName(GetRelativePath(item, relativePath));
                }
                else if (storePathMode == storeModeEnum.StoreFullPath)
                {
                    pathInArchive = null;
                }
                else
                {
                    pathInArchive = "";
                }

                _zip.AddFile(item, pathInArchive);
            }
        }