首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

閉關紀要:C# 結構化存儲在 Web 開發中的應用 (3)

     編輯:關於C語言

FileSystemFFS 完整實現代碼:結構化存儲核心邏輯

展示基於 BlueDog 庫的結構化存儲檔案系統實現,包含開啟/關閉、讀寫流、GZip 壓縮、資源管理完整流程。

FileSystemFFS.cs 核心代碼


using System;
using System.IO;
using System.Web;
using ExpertLib;
using ExpertLib.IO;
using ExpertLib.IO.Storage;
using System.Text.RegularExpressions;

namespace Step1.WebFileSystem {
    public class FileSystemFFS : FileSystem {
        private Storage ffsFile = null;
        private string filePath, ffsPath;
        private bool writeError = false;

        public FileSystemFFS(string path, Handler handle) : base(path, handle) {
            this.ffsPath = httpContext.Server.MapPath(Regex.Replace(path, handle.Pattern, handle.SavePath));
            this.filePath = Regex.Replace(path, handle.Pattern, handle.SaveName).Replace('/', '_');
        }

        private void Open() {
            ffsFile = StorageFile.OpenStorageFile(this.ffsPath);
        }

        public override bool Exists() {
            if (ffsFile == null) {
                FileInfo fileInfo = new FileInfo(this.ffsPath);
                if (!fileInfo.Exists) return false;
                Open();
            }
            return ffsFile.IsElementExist(this.filePath);
        }

        public override DateTime GetLastWriteTime() {
            if (ffsFile == null) {
                FileInfo fileInfo = new FileInfo(this.ffsPath);
                if (!fileInfo.Exists) return DateTime.Now;
                Open();
            }
            List elementsInfo = ffsFile.GetChildElementsInfo();
            for (int i = elementsInfo.Count - 1; i >= 0; i--) { // 注意原碼 i <= 0 應為 i >= 0
                if (elementsInfo[i].Name == filePath) return elementsInfo[i].LastModifyTime;
            }
            return DateTime.Now;
        }

        public override Stream GetWriter() {
            Close();
            if (ffsFile == null) {
                if (!File.Exists(this.ffsPath)) {
                    CreateFolder(this.ffsPath, false);
                    ffsFile = StorageFile.CreateStorageFile(this.ffsPath, StorageCreateMode.Create, 
                        StorageReadWriteMode.ReadWrite, StorageShareMode.ShareDenyWrite, StorageTransactedMode.Direct);
                } else {
                    Open();
                }
            }
            stream = (Stream)ffsFile.CreateStream(filePath);
            if (stream == null) { writeError = true; return base.GetWriter(); }
            if (this.handle.UseGzip) return new GZipOutputStream(stream);
            return stream;
        }

        public override Stream GetReader() {
            Close();
            if (ffsFile == null) {
                FileInfo fileInfo = new FileInfo(this.ffsPath);
                if (!fileInfo.Exists) return null;
                Open();
            }
            return stream = (Stream)ffsFile.OpenStream(filePath);
        }

        public override void TransmitFile() {
            if (writeError) base.TransmitFile();
            else this.TransmitStream(false);
        }

        public override void Dispose() {
            Close();
            if (ffsFile != null) { ffsFile.Dispose(); ffsFile = null; }
        }
    }
}

關鍵設計:

  • 延遲開啟策略:首次存取才開啟 Storage 檔案
  • GZip 串流包裝:UseGzip 設定決定是否壓縮
  • 錯誤降級:建立串流失敗回退基類實現 (直接輸出)
  • 資源釋放:Dispose 確保關閉串流與 Storage 檔案
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved