Example #1
0
        private static void ConvertChunkToEntry(IEnumerable <FileInfo> chunk, ConcurrentBag <FileEntryData> fileBag, string baseFolder)
        {
            foreach (FileInfo file in chunk)
            {
                FileEntryData  entryData  = new();
                FileLengthData lengthData = new();

                FileStream   fileStream    = file.OpenRead();
                MemoryStream fileMemStream = new();
                fileStream.CopyTo(fileMemStream);

                // Set the uncompressed length of the file
                lengthData.length = (int)fileStream.Length;

                // Check if the file is bigger than 1KB, and if it is, compress it
                // TODO: Convert compress required size to an option
                if (fileStream.Length > 1024 && ShouldCompress(file.Extension))
                {
                    byte[] compressedStream = FileUtilities.CompressFile(fileMemStream.ToArray());
                    lengthData.lengthCompressed = compressedStream.Length;
                    entryData.fileData          = compressedStream;
                }
                else
                {
                    lengthData.lengthCompressed = lengthData.length;
                    entryData.fileData          = fileMemStream.ToArray();
                }

                // Set the file name of the entry and the length data
                entryData.fileName       = Path.GetRelativePath(baseFolder, file.FullName).Replace('\\', '/');
                entryData.fileLengthData = lengthData;

                // Add the entry to the concurrent bag
                fileBag.Add(entryData);
            }
        }