DoCreateEntryFromFile() статический приватный Метод

static private DoCreateEntryFromFile ( ZipArchive destination, string sourceFileName, string entryName, CompressionLevel compressionLevel ) : ZipArchiveEntry
destination ZipArchive
sourceFileName string
entryName string
compressionLevel CompressionLevel
Результат ZipArchiveEntry
Пример #1
0
        private static void DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName,
                                                  CompressionLevel?compressionLevel, Boolean includeBaseDirectory,
                                                  Encoding entryNameEncoding)
        {
            // Rely on Path.GetFullPath for validation of sourceDirectoryName and destinationArchive

            // Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
            // as it is a pluggable component that completely encapsulates the meaning of compressionLevel.

            sourceDirectoryName        = Path.GetFullPath(sourceDirectoryName);
            destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);

            using (ZipArchive archive = Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
            {
                bool directoryIsEmpty = true;

                //add files and directories
                DirectoryInfo di = new DirectoryInfo(sourceDirectoryName);

                string basePath = di.FullName;

                if (includeBaseDirectory && di.Parent != null)
                {
                    basePath = di.Parent.FullName;
                }

                foreach (FileSystemInfo file in di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
                {
                    directoryIsEmpty = false;

                    Int32 entryNameLength = file.FullName.Length - basePath.Length;
                    Debug.Assert(entryNameLength > 0);

                    String entryName = EntryFromPath(file.FullName, basePath.Length, entryNameLength);

                    if (file is FileInfo)
                    {
                        // Create entry for file:
                        ZipFileExtensions.DoCreateEntryFromFile(archive, file.FullName, entryName, compressionLevel);
                    }
                    else
                    {
                        // Entry marking an empty dir:
                        DirectoryInfo possiblyEmpty = file as DirectoryInfo;
                        if (possiblyEmpty != null && IsDirEmpty(possiblyEmpty))
                        {
                            // FullName never returns a directory separator character on the end,
                            // but Zip archives require it to specify an explicit directory:
                            archive.CreateEntry(entryName + PathSeparator);
                        }
                    }
                }  // foreach

                // If no entries create an empty root directory entry:
                if (includeBaseDirectory && directoryIsEmpty)
                {
                    archive.CreateEntry(EntryFromPath(di.Name, 0, di.Name.Length) + PathSeparator);
                }
            } // using
        }     // DoCreateFromDirectory
Пример #2
0
        private static void DoCreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel?compressionLevel, bool includeBaseDirectory, Encoding entryNameEncoding)
        {
            string str;
            char   sPathSeperator;

            sourceDirectoryName        = Path.GetFullPath(sourceDirectoryName);
            destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);
            using (ZipArchive zipArchive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
            {
                bool          flag          = true;
                DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectoryName);
                string        fullName      = directoryInfo.FullName;
                if (includeBaseDirectory && directoryInfo.Parent != null)
                {
                    fullName = directoryInfo.Parent.FullName;
                }
                foreach (FileSystemInfo fileSystemInfo in directoryInfo.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
                {
                    flag = false;
                    int length = fileSystemInfo.FullName.Length - fullName.Length;
                    if (!LocalAppContextSwitches.ZipFileUseBackslash)
                    {
                        str = ZipFile.EntryFromPath(fileSystemInfo.FullName, fullName.Length, length);
                    }
                    else
                    {
                        str = fileSystemInfo.FullName.Substring(fullName.Length, length);
                        str = str.TrimStart(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                    }
                    if (!(fileSystemInfo is FileInfo))
                    {
                        DirectoryInfo directoryInfo1 = fileSystemInfo as DirectoryInfo;
                        if (directoryInfo1 == null || !ZipFile.IsDirEmpty(directoryInfo1))
                        {
                            continue;
                        }
                        sPathSeperator = ZipFile.s_pathSeperator;
                        zipArchive.CreateEntry(string.Concat(str, sPathSeperator.ToString()));
                    }
                    else
                    {
                        ZipFileExtensions.DoCreateEntryFromFile(zipArchive, fileSystemInfo.FullName, str, compressionLevel);
                    }
                }
                if (includeBaseDirectory & flag)
                {
                    str            = (LocalAppContextSwitches.ZipFileUseBackslash ? directoryInfo.Name : ZipFile.EntryFromPath(directoryInfo.Name, 0, directoryInfo.Name.Length));
                    sPathSeperator = ZipFile.s_pathSeperator;
                    zipArchive.CreateEntry(string.Concat(str, sPathSeperator.ToString()));
                }
            }
        }
Пример #3
0
        private static void DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName,
                                                  CompressionLevel?compressionLevel, Boolean includeBaseDirectory,
                                                  Encoding entryNameEncoding)
        {
            // Rely on Path.GetFullPath for validation of sourceDirectoryName and destinationArchive

            // Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
            // as it is a pluggable component that completely encapsulates the meaning of compressionLevel.

            sourceDirectoryName        = Path.GetFullPath(sourceDirectoryName);
            destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);

            using (ZipArchive archive = Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
            {
                bool directoryIsEmpty = true;

                //add files and directories
                DirectoryInfo di = new DirectoryInfo(sourceDirectoryName);

                string basePath = di.FullName;

                if (includeBaseDirectory && di.Parent != null)
                {
                    basePath = di.Parent.FullName;
                }

                // Windows' MaxPath (260) is used as an arbitrary default capacity, as it is likely
                // to be greater than the length of typical entry names from the file system, even
                // on non-Windows platforms. The capacity will be increased, if needed.
                const int DefaultCapacity = 260;
                char[]    entryNameBuffer = ArrayPool <char> .Shared.Rent(DefaultCapacity);

                try
                {
                    foreach (FileSystemInfo file in di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
                    {
                        directoryIsEmpty = false;

                        Int32 entryNameLength = file.FullName.Length - basePath.Length;
                        Debug.Assert(entryNameLength > 0);

                        if (file is FileInfo)
                        {
                            // Create entry for file:
                            String entryName = EntryFromPath(file.FullName, basePath.Length, entryNameLength, ref entryNameBuffer);
                            ZipFileExtensions.DoCreateEntryFromFile(archive, file.FullName, entryName, compressionLevel);
                        }
                        else
                        {
                            // Entry marking an empty dir:
                            DirectoryInfo possiblyEmpty = file as DirectoryInfo;
                            if (possiblyEmpty != null && IsDirEmpty(possiblyEmpty))
                            {
                                // FullName never returns a directory separator character on the end,
                                // but Zip archives require it to specify an explicit directory:
                                String entryName = EntryFromPath(file.FullName, basePath.Length, entryNameLength, ref entryNameBuffer, appendPathSeparator: true);
                                archive.CreateEntry(entryName);
                            }
                        }
                    }  // foreach

                    // If no entries create an empty root directory entry:
                    if (includeBaseDirectory && directoryIsEmpty)
                    {
                        archive.CreateEntry(EntryFromPath(di.Name, 0, di.Name.Length, ref entryNameBuffer, appendPathSeparator: true));
                    }
                }
                finally
                {
                    ArrayPool <char> .Shared.Return(entryNameBuffer);
                }
            } // using
        }     // DoCreateFromDirectory
Пример #4
0
 public static ZipArchiveEntry CreateEntryFromFile(this ZipArchive destination, string sourceFileName, string entryName, CompressionLevel compressionLevel)
 {
     return(ZipFileExtensions.DoCreateEntryFromFile(destination, sourceFileName, entryName, new CompressionLevel?(compressionLevel)));
 }
Пример #5
0
 public static ZipArchiveEntry CreateEntryFromFile(this ZipArchive destination, string sourceFileName, string entryName)
 {
     return(ZipFileExtensions.DoCreateEntryFromFile(destination, sourceFileName, entryName, null));
 }