Bloom.CollectionTab.LibraryModel.CompressDirectory C# (CSharp) Method

CompressDirectory() protected static method

Adds a directory, along with all files and subdirectories, to the ZipStream.
Protected for testing purposes
protected static CompressDirectory ( string directoryPath, ZipOutputStream zipStream, int dirNameOffest, bool forReaderTools ) : void
directoryPath string The directory to add recursively
zipStream ICSharpCode.SharpZipLib.Zip.ZipOutputStream The ZipStream to which the files and directories will be added
dirNameOffest int This number of characters will be removed from the full directory or file name /// before creating the zip entry name
forReaderTools bool If True, then some pre-processing will be done to the contents of decodable /// and leveled readers before they are added to the ZipStream
return void
        protected static void CompressDirectory(string directoryPath, ZipOutputStream zipStream, int dirNameOffest,
			bool forReaderTools)
        {
            if (Path.GetFileName(directoryPath).ToLowerInvariant() == "audio")
                return; // don't want audio in bloompack. todo: test
            var files = Directory.GetFiles(directoryPath);
            var bookFile = BookStorage.FindBookHtmlInFolder(directoryPath);

            foreach (var filePath in files)
            {
                if (excludedFileExtensionsLowerCase.Contains(Path.GetExtension(filePath.ToLowerInvariant())))
                    continue; // BL-2246: skip putting this one into the BloomPack

                FileInfo fi = new FileInfo(filePath);

                var entryName = filePath.Substring(dirNameOffest);  // Makes the name in zip based on the folder
                entryName = ZipEntry.CleanName(entryName);          // Removes drive from name and fixes slash direction
                ZipEntry newEntry = new ZipEntry(entryName) { DateTime = fi.LastWriteTime };
                newEntry.IsUnicodeText = true; // encode filename and comment in UTF8
                byte[] modifiedContent = {};

                // if this is a ReaderTools book, call GetBookReplacedWithTemplate() to get the contents
                if (forReaderTools && (bookFile == filePath))
                {
                    modifiedContent = Encoding.UTF8.GetBytes(GetBookReplacedWithTemplate(filePath));
                    newEntry.Size = modifiedContent.Length;
                }
                else if (forReaderTools && (Path.GetFileName(filePath)=="meta.json"))
                {
                    modifiedContent = Encoding.UTF8.GetBytes(GetMetaJsonModfiedForTemplate(filePath));
                    newEntry.Size = modifiedContent.Length;
                }
                else
                {
                    newEntry.Size = fi.Length;
                }

                zipStream.PutNextEntry(newEntry);

                if (modifiedContent.Length > 0)
                {
                    using (var memStream = new MemoryStream(modifiedContent))
                    {
                        StreamUtils.Copy(memStream, zipStream, new byte[modifiedContent.Length]);
                    }
                }
                else
                {
                    // Zip the file in buffered chunks
                    byte[] buffer = new byte[4096];
                    using (var streamReader = RobustFile.OpenRead(filePath))
                    {
                        StreamUtils.Copy(streamReader, zipStream, buffer);
                    }
                }

                zipStream.CloseEntry();
            }

            var folders = Directory.GetDirectories(directoryPath);

            foreach (var folder in folders)
            {
                var dirName = Path.GetFileName(folder);
                if ((dirName == null) || (dirName.ToLowerInvariant() == "sample texts"))
                    continue; // Don't want to bundle these up

                CompressDirectory(folder, zipStream, dirNameOffest, forReaderTools);
            }
        }