kbmmw 中一直有一个功能,但是基本上都没有提过,但是在实际应用中,却非常有用,这个功能就是

虚拟文件包功能,他可以把一大堆文件保存到一个文件里面,方便后台管理。

kbmmw 的虚拟文件在单元kbmMWStreamStore 中实现,这个是非控件形式的,因此需要手工添加这个

单元。另外虚拟文件操作的类为TkbmMWLookupStorage,所有的操作都由这个类实现,具体可以参加源码。

因为这个很简单,直接就上界面

具体实现代码如下

procedure TForm2.Button1Click(Sender: TObject);// 打包
var
sr:TSearchRec;
i:integer;
fs:TFileStream;
fsfile:TFileStream;
st:TkbmMWLookupStorage;
begin
fs:=TFileStream.Create(edit2.Text,fmCreate or fmOpenReadWrite);
try
st:=TkbmMWLookupStorage.Create(fs);
try chdir(edit1.Text);
i:=FindFirst('*.*',faNormal,sr);
while i= do
begin
try
fsfile:=TFileStream.Create(sr.Name,fmOpenRead or fmShareCompat);
try
st.Add(sr.Name,fsfile);
finally
fsfile.Free;
end;
except
end;
i:=FindNext(sr);
end;
finally
st.Free;
end;
finally
fs.Free;
end; showmessage('打包成功'); end; procedure TForm2.Button2Click(Sender: TObject); //列目录
var
fs:TFileStream;
st:TkbmMWLookupStorage;
begin
fs:=TFileStream.Create(edit2.text,fmOpenReadWrite);
try
st:=TkbmMWLookupStorage.Create(fs);
try
Label1.Caption:=inttostr(st.Count);
st.GetIdentifiers(ListBox1.Items);
finally
st.Free;
end;
finally
fs.Free;
end; end; procedure TForm2.Button3Click(Sender: TObject);//解压文件
var
i:integer;
fs:TFileStream;
fsfile:TFileStream;
st:TkbmMWLookupStorage;
sl:TStringList;
s:string;
begin
fs:=TFileStream.Create(edit2.Text,fmOpenReadWrite);
try
st:=TkbmMWLookupStorage.Create(fs);
try
sl:=TStringList.Create;
try
st.GetIdentifiers(sl);
for i:= to sl.Count- do
begin
s:=sl.Strings[i];
fsfile:=TFileStream.Create(edit3.Text+s,fmCreate or fmOpenWrite);
try
st.Get(s,fsfile);
finally
fsfile.Free;
end;
end; finally
sl.Free;
end;
finally
st.Free;
end;
finally
fs.Free;
end; showmessage('解包成功!'); end;

运行效果

可以看见一共319 个文件

同时在d:\temp 生成了一个so 文件

打包成功

列表可以显示包里面的文件

解压结果

解压正确。

有的时候我们为了保密和减小文件大小,可以采用加密和压缩。

下面再演示一下加密的方式。

加入一个kbmmwcrypt和两个按钮。

对应的代码如下:

procedure TForm2.Button4Click(Sender: TObject);    //加密打包
var
sr:TSearchRec;
i:integer;
ms:Tbytesstream;
fs:TFileStream;
fsfile:TFileStream;
st:TkbmMWLookupStorage;
begin
fs:=TFileStream.Create(edit2.Text,fmCreate or fmOpenReadWrite); ms:=TbytesStream.Create; try
st:=TkbmMWLookupStorage.Create(ms);
try chdir(edit1.Text);
i:=FindFirst('*.*',faNormal,sr);
while i= do
begin
try
fsfile:=TFileStream.Create(sr.Name,fmOpenRead or fmShareCompat);
try
st.Add(sr.Name,fsfile);
finally
fsfile.Free;
end;
except
end;
i:=FindNext(sr);
end; kbmMWCrypt1.PassPhrase:='xalion123456';
ms.Position:=;
kbmMWCrypt1.Encrypt(nil,ms,fs); finally
st.Free;
end; finally
fs.Free;
ms.Free;
end; showmessage('打包成功'); end; procedure TForm2.Button5Click(Sender: TObject); // 解密展开
var
i:integer;
fs:TFileStream;
fsfile:TFileStream;
ms:Tbytesstream;
st:TkbmMWLookupStorage;
sl:TStringList;
s:string;
begin
fs:=TFileStream.Create(edit2.Text,fmOpenReadWrite);
ms:=TbytesStream.Create;
kbmMWCrypt1.PassPhrase:='xalion123456';
kbmMWCrypt1.decrypt(nil,fs,ms); try
st:=TkbmMWLookupStorage.Create(ms);
try
sl:=TStringList.Create;
try
st.GetIdentifiers(sl);
for i:= to sl.Count- do
begin
s:=sl.Strings[i];
fsfile:=TFileStream.Create(edit3.Text+s,fmCreate or fmOpenWrite);
try
st.Get(s,fsfile);
finally
fsfile.Free;
end;
end; finally
sl.Free;
end;
finally
st.Free;
end;
finally
fs.Free;
end; showmessage('解包成功!'); end;

运行程序,与没有加密的运行效果一致,但是生成的文件已经被加密。

别人即使偷走文件,也不用担心信息泄露了。

kbmmw 中虚拟文件操作入门的更多相关文章

  1. 第32课 Qt中的文件操作

    1. Qt的中IO操作 (1)Qt中IO操作的处理方式 ①Qt通过统一的接口简化了文件和外部设备的操作方式 ②Qt中的文件被看作一种特殊的外部设备 ③Qt中的文件操作与外部设备的操作相同 (2)IO操 ...

  2. 003-Tuple、Array、Map与文件操作入门实战

    003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...

  3. 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作

    原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...

  4. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  5. 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理

    [源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...

  6. Java中的文件操作(一)RandomAccessFile

    今天,学到的是java中的文件操作. Java.IO.File Java中操作文件用到RandomAccessFile类,既可以读取文件内容,也可以向文件输出数据,但不同与普通输入/输出流的是Rand ...

  7. Win 32平台SDK中的文件操作

    读取文件: HANDLE hFile ; // 声明文件操作内核对象句柄 hFile = CreateFile(, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ...

  8. 关于文件的INode与Java中的文件操作接口

    本文由作者周梁伟授权网易云社区发布. 近日做的项目中涉及到多进程共同读写多个文件的问题,文件名和最后修改时间都是可能会被频繁修改的,因而识别文件的唯一性会产生相当的麻烦,于是专门再学习了一下文件系统对 ...

  9. ASP.NET中的文件操作(文件信息,新建,移动,复制,重命名,上传,遍历)(亲测详细)

    做了几天的文件操作,现在来总结一下,错误之处,还望指点!以文件为例,如果对文件夹操作,基本上将File换为Directory即可(例:FileInfo file = new FileInfo(Path ...

随机推荐

  1. Ant 使用指南 与 知识汇总

    一.Ant是什么?        Ant是一种基于Java和XML的build工具.它可以帮助我们将项目开发过程中需要完成的各种步骤组织起来,通过一个简易的方式来构建整个项目.Ant究竟能做什么呢?这 ...

  2. PHP遍历数组常用方式(for,foreach,while,指针等等)

    1使用for循环遍历数组 count($arr)用于统计数组元素个数         for循环只能用于遍历,纯索引数组!!如果存在关联数组,count统计两种数组的总个数         使用for ...

  3. 三个<li>元素放一行

    <ul><li style="float:left;display:inline;">0</li><li style="floa ...

  4. oracle中取得当前日期,前一天,当前月,前一个月

    当前日:select TRUNC(SYSDATE)  from dual; 前一天: select TRUNC(SYSDATE - 1)   from dual; 前一天转换为日期格式: select ...

  5. ES6之对象的简洁表示法

    ES6 允许直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. let name = 'Pirates of the Caribbean', index = 5, captain = { ...

  6. Q in Q

    简介 Q in Q技术(也称Stacked VLAN 或Double VLAN).标准出自IEEE 802.1ad,将用户私网VLAN Tag封装在公网VLAN Tag中,使报文带着两层VLAN Ta ...

  7. 865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树

    [抄题]: Given a binary tree rooted at root, the depth of each node is the shortest distance to the roo ...

  8. VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到

    VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到 添加完之后要等一会儿 等一会儿 等一会儿 就有了

  9. Extract Dataset

    FROM <SAP PROGRAM DESIGN> Extract Datasets简称为Extract,是SAP中除了内表之外的另一种大量内存数据处理方式,允许用户动态地存储并排序结构化 ...

  10. Django创建模型,迁移数据

    1.在models.py文件中添加代码 class notice(models.Model): notice_title = models.CharField(max_length=255) noti ...