如何在Node js中保存以字符串形式表示的zip文件 [英] How to save zip file represented as a string in Node js

查看:150
本文介绍了如何在Node js中保存以字符串形式表示的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到ebay-api的回复

I have a response from an ebay-api

-MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375576​​565344内容类型:application/xop + xml; charset = utf-8; type ="text/xml" Content-Transfer-Encoding:二进制Content-ID: < 0.urn:uuid:C91296EA5FF69EE9571479882375576​​565345>

--MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375576565344 Content-Type: application/xop+xml; charset=utf-8; type="text/xml" Content-Transfer-Encoding: binary Content-ID: <0.urn:uuid:C91296EA5FF69EE9571479882375576565345>

成功1.1.02016-11-23T06:26:15.576Z514 --MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375574545344内容类型:application/zip内容传输编码:二进制 内容ID:

Success1.1.02016-11-23T06:26:15.576Z514 --MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375574545344 Content-Type: application/zip Content-Transfer-Encoding: binary Content-ID:

PKY'uIi [ @ 50014028337_report.xmlUTy 2Xy 2Xux 00 R j @ + [ PlX# ( x,=l q]Lfewc wĤ O ١ HT t GGT. 6 ; .$ =d m; c} Wߦ RW A f g I 4U x 3 f ғ{f xj ,+ ۖ I%5 B的 G,# t ,L { c MD笓 )! 9 M o;8_

PKY'uIi[��@�50014028337_report.xmlUT y�2Xy�2Xux 00�R�j�@��+��[��PlX#�(�x,=l�q]Lfewc��w Ĥ��O��١�HT���t��GGT� ��6�;���'������.$����=d����m;c}Wߦ�RW�A f�����g�I��4U��x��3��f���ғ{f��xj�,+���ۖI%5��B's��G,#��t,L{�c�����MD笓��)!�9�� �M�o;8_��<�i�y����sz���u���=��Ջ^2�S��%+2�2�`QV�$�����~?�w�ǥ�_Q�퉦�'PKY'uIi[��@���50014028337_report.xmlUTy�2Xux 00PK\� --MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375576565344--

这是字符串类型.然后我提取了附件的zip文件数据,即

This is of type string. and i extracted the attached zip file data i.e.

PKY'uIi [ @ 50014028337_report.xmlUTy 2Xy 2Xux 00 R j @ + [ PlX# ( x,=l q]Lfewc wĤ O ١ HT t GGT. 6 ; .$ =d m; c} Wߦ RW A f g I 4U x 3 f ғ{f xj ,+ ۖ I%5 B的 G,# t ,L { c MD笓 )! 9 M o;8_

PKY'uIi[��@�50014028337_report.xmlUT y�2Xy�2Xux 00�R�j�@��+��[��PlX#�(�x,=l�q]Lfewc��w Ĥ��O��١�HT���t��GGT� ��6�;���'������.$����=d����m;c}Wߦ�RW�A f�����g�I��4U��x��3��f���ғ{f��xj�,+���ۖI%5��B's��G,#��t,L{�c�����MD笓��)!�9�� �M�o;8_��<�i�y����sz���u���=��Ջ^2�S��%+2�2�`QV�$�����~?�w�ǥ�_Q�퉦�'PKY'uIi[��@���50014028338_report.xmlUTy�2Xux 00PK\�

这表明其中包含report.xml.因此,当我将这些数据写入zip文件时,它会创建一个zip文件,并且在提取时会出现错误.

This shows that it has a report.xml in it. So when i write this data in a zip file, it creates a zip file and upon extract gives error.

fs.writeFile("./static/DownloadFile.zip", fileData, 'binary', function(err){
                  if (err) throw err;
                  console.log("success");
                  });

我该如何正确地将这些数据写入zip文件中.请咨询.如果需要更多信息.

How can i write this data in a zip file properly. Pls advice. If required any more information.

我尝试用PHP编写zip文件,并使用以下代码成功编写了该文件:

I tried writing the zip file in PHP and is succssfully writing it with this code:

$zipFilename="DownloadFile.zip";
        $data       =   $fileData;
        $handler = fopen($zipFilename, 'wb')
            or die("Failed. Cannot Open $zipFilename to Write!</b></p>");
        fwrite($handler, $data);
        fclose($handler);

请告知我如何在Node.js中实现相同的目标.

Please advice how can i achieve the same thing in nodejs.

推荐答案

正如我在您的代码示例中看到的那样,您的二进制数据已被请求模块处理.只需在请求设置中使用

As I see in your code example your binary data is already mangled by request module. Just use in request setting

encoding:null

,并且zip文件是有效的二进制文件(现在是缓冲区,而不是utf-8字符串!),您可以进行解压缩.只要您看到问题标记,您仍然会遇到编码问题.

and the zip file is a valid binary in body (now buffer instead of utf-8 string!) you can decompress. As long as you see the questions marks you still have the encoding issue.

这篇关于如何在Node js中保存以字符串形式表示的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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