Esempio n. 1
0
        public override byte[] GetBinaryFile(string fileName, bool cache = false)
        {
            if (ArchiveMap != null)
            {
                lock (BinaryFileLock)
                {
                    FI fi = ArchiveMap.FindString(ref fileName, out int size);
                    if (!string.IsNullOrWhiteSpace(fileName))
                    {
                        if (LocalTryGetValue(fileName, out BufferWithAge value))
                        {
                            Memory.Log.WriteLine($"{nameof(ArchiveZzz)}::{nameof(GetBinaryFile)}::{nameof(CacheTryGetValue)} read from cache {fileName}");
                            return(value);
                        }
                        else
                        {
                            Stream s;
                            if ((s = OpenStream()) != null)
                            {
                                byte[] buffer = ArchiveMap.GetBinaryFile(fi, s, fileName, size);
                                if (buffer != null && cache && LocalTryAdd(fileName, buffer))
                                {
                                    Memory.Log.WriteLine($"{nameof(ArchiveZzz)}::{nameof(GetBinaryFile)}::{nameof(LocalTryAdd)} caching {fileName}");
                                }

                                return(buffer);
                            }
                        }
                    }
                }
            }
            Memory.Log.WriteLine($"{nameof(ArchiveZzz)}::{nameof(GetBinaryFile)} FAILED extracting {fileName}");
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Get binary data
        /// </summary>
        /// <param name="fileName">filename you want</param>
        /// <param name="cache">if true store the data for later</param>
        /// <returns></returns>
        public override byte[] GetBinaryFile(string fileName, bool cache = false)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new FileNotFoundException("NO FILENAME");
            }
            byte[] FileInTwoArchives()
            {
                //if (FS != null && FS.Length > 0)
                //    return ArchiveMap.GetBinaryFile(filename, new MemoryStream(FS));
                //else
                return(FsArchive != null?ArchiveMap.GetBinaryFile(fileName, FsArchive.GetStreamWithRangeValues(Archive.FS)) : null);
            }

            if (ArchiveMap != null && ArchiveMap.Count > 0)
            {
                if (!cache)
                {
                    return(FileInTwoArchives());
                }
                var fileNameBackup = fileName;
                GetListOfFiles();
                fileName = FileList.OrderBy(x => fileName.Length).ThenBy(x => fileNameBackup, StringComparer.OrdinalIgnoreCase).FirstOrDefault(x => x.IndexOf(fileNameBackup, StringComparison.OrdinalIgnoreCase) >= 0);

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new NullReferenceException($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} fileName ({fileNameBackup}) not found!");
                }

                if (!LocalTryGetValue(fileName, out var value))
                {
                    var buffer = FileInTwoArchives();
                    if (LocalTryAdd(fileName, buffer))
                    {
                        Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)}::{nameof(LocalTryAdd)} cached {fileName}");
                    }
                    return(buffer);
                }
                Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)}::{nameof(LocalTryGetValue)} read from cache {fileName}");
                return(value);
            }

            if (!IsDir)
            {
                var loc = -1;
                if (File.Exists(Archive.FL))
                {
                    using (var fs = new FileStream(Archive.FL, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        loc = FindFile(ref fileName, fs); //File.OpenRead(archive.FL));
                }
                // read file list

                if (loc == -1)
                {
                    Debug.WriteLine($"ArchiveWorker: NO SUCH FILE! :: {Archive.FL}");
                    //throw new Exception("ArchiveWorker: No such file!");
                }
                else
                {
                    return(GetBinaryFile(fileName, loc, cache));
                }
            }
            else
            {
                return(GetBinaryFile(fileName, 0, cache));
            }

            Debug.WriteLine($"ArchiveWorker: NO SUCH FILE! :: Searched {Archive} and could not find {fileName}");
            return(null);
        }