英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论