Exemplo n.º 1
0
        /// <summary>
        /// Get all the entries.
        /// </summary>
        /// <param name="zipArchive">The archived zip stream to get data from.</param>
        /// <returns>The archive entry collection.</returns>
        public List <ArchiveEntry> GetEntries(Stream zipArchive)
        {
            List <ArchiveEntry> archiveEntries = new List <ArchiveEntry>();

            // Open the archive.
            using (ZipArchive archive = new ZipArchive(zipArchive, ZipArchiveMode.Read, true))
            {
                // For each entry.
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    // Create a new archive entry.
                    ArchiveEntry archiveEntry = new ArchiveEntry();

                    // Get the entry details.
                    archiveEntry.Name              = entry.Name;
                    archiveEntry.Length            = entry.Length;
                    archiveEntry.FullName          = entry.FullName;
                    archiveEntry.LastWriteTime     = entry.LastWriteTime;
                    archiveEntry.CompressionLength = entry.CompressedLength;

                    // Get the entry stream.
                    archiveEntry.Stream = entry.Open();

                    // Add the entry.
                    archiveEntries.Add(archiveEntry);
                }
            }

            // Return the archive streams.
            return(archiveEntries);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the entry.
        /// </summary>
        /// <param name="zipArchive">The archived zip stream to get data from.</param>
        /// <param name="entryName">The entry name to search for.</param>
        /// <returns>The archive entry.</returns>
        public ArchiveEntry GetEntry(Stream zipArchive, string entryName)
        {
            ArchiveEntry archiveEntry = new ArchiveEntry();

            // Open the archive.
            using (ZipArchive archive = new ZipArchive(zipArchive, ZipArchiveMode.Read, true))
            {
                // For each entry.
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    // If the entry has been found.
                    if (entry.FullName.ToLower() == entryName.ToLower())
                    {
                        // Get the entry details.
                        archiveEntry.Name              = entry.Name;
                        archiveEntry.Length            = entry.Length;
                        archiveEntry.FullName          = entry.FullName;
                        archiveEntry.LastWriteTime     = entry.LastWriteTime;
                        archiveEntry.CompressionLength = entry.CompressedLength;

                        // Get the entry stream.
                        archiveEntry.Stream = entry.Open();
                        break;
                    }
                }
            }

            // Return the archive stream.
            return(archiveEntry);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the entries.
        /// </summary>
        /// <param name="zipArchive">The archived zip stream to get data from.</param>
        /// <param name="entryNames">The entry names to search for.</param>
        /// <returns>The archive entry collection.</returns>
        public List <ArchiveEntry> GetEntries(Stream zipArchive, string[] entryNames)
        {
            List <ArchiveEntry> archiveEntries = new List <ArchiveEntry>();

            // Open the archive.
            using (ZipArchive archive = new ZipArchive(zipArchive, ZipArchiveMode.Read, true))
            {
                // For each entry to find.
                foreach (string name in entryNames)
                {
                    // For each entry.
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        // If the entry has been found.
                        if (entry.FullName.ToLower() == name.ToLower())
                        {
                            // Create a new archive entry.
                            ArchiveEntry archiveEntry = new ArchiveEntry();

                            // Get the entry details.
                            archiveEntry.Name              = entry.Name;
                            archiveEntry.Length            = entry.Length;
                            archiveEntry.FullName          = entry.FullName;
                            archiveEntry.LastWriteTime     = entry.LastWriteTime;
                            archiveEntry.CompressionLength = entry.CompressedLength;

                            // Get the entry stream.
                            archiveEntry.Stream = entry.Open();

                            // Add the entry.
                            archiveEntries.Add(archiveEntry);
                            break;
                        }
                    }
                }
            }

            // Return the archive streams.
            return(archiveEntries);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Unzips the archive entry to the specified location.
        /// </summary>
        /// <param name="archiveEntry">The archive entry.</param>
        /// <param name="unZipDirectorPath">The directory where to un-zip the archive entry.</param>
        public static void Decompress(ArchiveEntry archiveEntry, string unZipDirectorPath)
        {
            // Get the directory path.
            string directory = Path.GetDirectoryName(unZipDirectorPath).TrimEnd('\\') + "\\";

            // Create the relative directory if
            // it does not exist
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // Get the stream.
            using (Stream stream = archiveEntry.Stream)
            {
                // Create a new file stream.
                using (FileStream writer = new FileStream(directory + archiveEntry.FullName, FileMode.Create, FileAccess.Write))
                {
                    // Write the entry data to the file.
                    stream.CopyTo(writer);
                }
            }
        }