C:理论如何从归档文件中提取文件 [英] C: theory on how to extract files from an archived file

查看:123
本文介绍了C:理论如何从归档文件中提取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C我创建了一个程序,它可以归档多个文件到通过命令行的归档文件。
例如。

In C I have created a program which can archive multiple files into an archive file via the command line. e.g.

$echo 'file1/2' > file1/2.txt
$./archive file1.txt file2.txt arhivedfile
$cat archivedfile 
file1
file2

如何创建一个过程,以便在我archivedfile我有:

How do I create a process so that in my archivedfile I have:

header
file1
end
header
file2
end

它们都存储在归档文件一个又一个接一个。我知道,也许需要一个头文件中提取这些文件背出到其原来的形式(包含文件名,文件名的大小,启动和文件结束),但我将如何去这样做。

They are all stored in the archive file one after another after another. I know that perhaps a header file is needed(containing filename, size of filename, start and end of file) for extracting these files back out into their original form, but how would I go about doing this.

我是卡在哪里以及如何下手。

I am stuck on where and how to start.

请能有人帮我一些逻辑为如何处理压缩文件背出归档文件的。

Please could someone help me on some logic as to how to approach extracting files back out of an archived file.

推荐答案

,开始与算法。你已经有大部分的细节问题。

As has been mentioned before, start with the algorithm. You already have most of the details.

有您可以采取一些方法:

There are a few approaches you can take:


  1. 随机存取档案。

  2. 顺序存取档案。

有关这个工作,首标需要以充当索引(如在库中的卡索引),指示; (a)在以找到每个文件的开始;和(b)的每个文件的长度。写归档文件的算法可能是这样的:

Random Access Archive

For this to work, the header needs to act as an index (like the card indexes at a library), indicating; (a) where to find the start of each file; and (b) the length of each file. The algorithm to write the archive file might look like:


  1. 获取命令行中的所有文件的清单。

  2. 创建保存有关每个文件的元数据结构:名称(255字符),大小(64位int),日期和时间,和权限

  3. 对于每个文件,获取其统计数据。

  4. 保存每个文件的统计结构的数组中。

  5. 打开存档写作。

  6. 写头结构。

  7. 对于每个文件,追加其内容的存档文件。

  8. 关闭存档文件。

(头可能需要包括的文件的数量,也。)

(The header might have to include the number of files, too.)

接下来,该算法用于提取文件:

Next, the algorithm for extracting files:


  1. 获取命令行的归档文件。

  2. 获取文件名中提取,还命令行。

  3. 的结构创建存储器中读取有关每个文件的元数据。

  4. 从存档文件中读取所有元数据。

  5. 搜索文件名遍布元数据的列表中提取。

  6. 计算偏移到用于匹配的文件名开头的存档文件。

  7. 寻到抵消。

  8. 读取文件内容并写入到一个新文件。

  9. 关闭新的文件。

  10. 关闭档案。

这是更容易。你可以自己做。想通过步骤

Sequential Access

This is easier. You can do it yourself: think through the steps.

这是很容易在的如何的东西应该工作的细节被套牢。我建议你​​退一步 - 这是你的老师在课堂上应该讨论 - 并去想想上面的编码水平的问题,因为:

It is easy to get caught up in the details of how something should work. I suggest that you take a step back -- something your teacher should discuss in class -- and try to think about the problem at a level above coding, because:


  • 您创建将独立语言算法;

  • 在算法固定的错误,在此之前code被写入,是微不足道的;

  • 您将有一个更好的了解你需要编写代码之前要做什么;

  • 将花费更少的时间来实施解决方案;

  • 您可以识别可并行执行领域;

  • 您会看到时间提前任何潜在障碍;和

  • 您会用自己的方式在任何时间管理职位。 ; - )

这篇关于C:理论如何从归档文件中提取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆