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);
}
}