解压 Dlang 中的存档。

huangapple go评论57阅读模式
英文:

Unzip archive in Dlang

问题

I'm trying to unpack zip archive in Dlang, but all methods what im tried isnt work.

I'm tried to:

auto archive = "/var/cache/dpm/packages/" ~ packagename ~ "/" ~ packagename ~ ".zip";
auto zip = new ZipArchive(read(archive));
writeln(read(archive));

Expected to get what archive contains, but get only many numbers. (You can see it on screenshot)
What is happens
After this, i tried to readText instead of read because i see something like this when tried to read file with read and not readText, but it gives me error:
tf.d(1556): Invalid UTF-8 sequence (at index 1)
Where is my error?
Thanks for your help!

英文:

I'm trying to unpack zip archive in Dlang, but all methods what im tried isnt work.

I'm tried to:
auto archive = "/var/cache/dpm/packages/"~packagename~"/"~packagename~".zip";
auto zip = new ZipArchive(read(archive));
writeln(read(archive));

Excepted to get what archive contains, but get only many numbers.(You can see it on screenshot)
What is happens
After this, i tried to readText instead of read because i see something like this when tried to read file with read and not readText, but it gives me error:
tf.d(1556): Invalid UTF-8 sequence (at index 1)
Where is my error?
Thanks for your help!

答案1

得分: 0

以下是翻译的内容:

你所看到的是一个 ubyte[] 数组的默认输出。它是以十进制形式打印的字节列表。

read(archive) 会给你文件中的 ubytes,没有别的。你两次读取文件,第一次创建一个存档(成功),第二次将其读入一个 ubyte[] 数组并打印出来。

如果你想要获取zip文件的内容,你需要使用 ZipArchive API 来实现。看起来可以使用 zip.directory["fileToExtract"].expandedData 来实现。

请查看 ZipArchive 的文档,并在这里查看如何使用它的示例:https://dlang.org/phobos/std_zip.html

英文:

What you are seeing is the default output of a ubyte[] array. It's a list of bytes, printed in decimal form.

read(archive) gives you the ubytes in the file. Nothing else. You are reading the file twice, the first time creating an archive out of it (which succeeds), and the second time reading it into a ubyte[] array and printing that.

If you want the zip contents, you have to use the ZipArchive api to do it. Looks like that would be zip.directory["fileToExtract"].expandedData

See the documentation for ZipArchive, and an example how to use it here: https://dlang.org/phobos/std_zip.html

huangapple
  • 本文由 发表于 2023年5月7日 22:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定