【问题标题】:Merge Multiple text files into one text file (and back to original files) lua and php将多个文本文件合并为一个文本文件(并返回原​​始文件)lua 和 php
【发布时间】:2014-03-04 00:41:26
【问题描述】:

基本上我有一个 lua 客户端和一个 php 服务器。假设我在服务器上有一个包含多个文本文件的目录,例如:

  • 服务器上的目录
    • 文件1
    • 文件2
    • 文件3

我想获取一个目录中的所有文件并将它们合并为一个,但是我希望它能够获取文件并将文件与名称一起放回原处。在压缩文件中的含义必须有每个段的标题(来自文件的文本)例如

压缩文本文件:

header for file1 to identify it for later use
file1's contents here
header for file2 to identify it for later use
file2's contents here
header for file3 to identify it for later use
file3's contents here

这样可以将内容和名称放回原处。

PHP:我需要 php 将文件夹内容压缩为一个文本文件(所有文件夹项都是文本文件),以便我可以将其发送到 lua 客户端

LUA:我需要 lua 能够从 php 服务器获取数据并将文件解压缩到指定文件夹中

我不想使用实际的压缩算法,我只想将所有文本文件合二为一,然后将它们放回去。谢谢

【问题讨论】:

    标签: php file text lua


    【解决方案1】:

    用 PHP 中的标记连接文件应该很简单,比如

    $fpAll = fopen("fileAll", w+);
    $file1 = file_get_contents("file1");
    $file2 = file_get_contents("file2");
    $h = "# some file separation marker line";
    $all = $h + "\n" + $file1 + $h + "\n" + $file2 ...;
    ... send $all to client ...
    

    在 Lua 方面,假设一个 HTTP 库以类文件对象的形式提供响应,

    separatonMarker = "# some file separation marker line"
    files = {}
    fileCount = 0
    for line in response:lines() do
         if line == separationMarker then
             nextFile = {}
             fileCount = fileCount + 1
             files[fileCount] = nextFile
         else
             table.insert(nextFile, line) -- append to end
         end
    end
    

    如果您想在本地保存文件,您可以在创建新的 nextFile 之前打开一个文件,并将 nextFile 中的每一行保存到其中。

    【讨论】:

    • 谢谢!谢谢,这是我需要的。
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    相关资源
    最近更新 更多