te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to generate zip file using Buffer contents in node.js using JSZip? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to generate zip file using Buffer contents in node.js using JSZip? - Stack Overflow

programmeradmin3浏览0评论

I have an array of strings which should be written to a .txt file. Also I need to press the resulting .txt file to .zip format using JSZip. At the client side, I was able to generate a 'text/plain' Blob using this array of strings and then I pressed this Blob to .zip format using JSZip. I need to do the same on the server side using node.js, but I realized that Blob is not available in node.js. I tried using 'Buffer' instead of Blob, and I got a binary file pressed as .zip; I am a beginner with node.js and couldn't understand the concepts of Buffer. Can I create a Blob in node.js? or Can I perform the same operations with node.js Buffer?

At the client side I can generate the zip file from Blob contents like this,

//stringsArray is an array of strings
var blob = new Blob( stringsArray, { type: "text/plain" } );

var zip = new JSZip();
zip.file( 'file.txt' , blob );

zip.generateAsync( { type: 'blob', pression: 'DEFLATE' } )
.then( function( zipFile ){ 

    //do something with zipFile 

}, function( error ){ 

    console.log( 'Error in pression' );

} );

How can I do the same using Node.js?

I have an array of strings which should be written to a .txt file. Also I need to press the resulting .txt file to .zip format using JSZip. At the client side, I was able to generate a 'text/plain' Blob using this array of strings and then I pressed this Blob to .zip format using JSZip. I need to do the same on the server side using node.js, but I realized that Blob is not available in node.js. I tried using 'Buffer' instead of Blob, and I got a binary file pressed as .zip; I am a beginner with node.js and couldn't understand the concepts of Buffer. Can I create a Blob in node.js? or Can I perform the same operations with node.js Buffer?

At the client side I can generate the zip file from Blob contents like this,

//stringsArray is an array of strings
var blob = new Blob( stringsArray, { type: "text/plain" } );

var zip = new JSZip();
zip.file( 'file.txt' , blob );

zip.generateAsync( { type: 'blob', pression: 'DEFLATE' } )
.then( function( zipFile ){ 

    //do something with zipFile 

}, function( error ){ 

    console.log( 'Error in pression' );

} );

How can I do the same using Node.js?

Share Improve this question asked Sep 25, 2017 at 9:40 HariVHariV 7071 gold badge11 silver badges17 bronze badges 3
  • 1 This code should run as is. Perhaps check the documentation for JSZip if they have a different method for Node. – user2652134 Commented Sep 25, 2017 at 10:15
  • @BrahmaDev , I got 'Blob is not defined' error in node.js – HariV Commented Sep 25, 2017 at 10:47
  • 1 Sorry for that, you'd have to use Buffer. The documentation would be a good point to research around this : stuk.github.io/jszip/documentation/api_jszip/… – user2652134 Commented Sep 25, 2017 at 10:51
Add a ment  | 

1 Answer 1

Reset to default 12

I found a solution for this. In my code, I wasn't using the proper method to convert the array of strings to node.js buffer ( I wasn't able to press the buffer using JSZip because the buffer was incorrect ). I tried the following code but it gave me an incorrect buffer,

//stringsArray is an array of strings
var buffer = Buffer.from( stringsArray );

The proper way is that, we have to convert each string to buffer first and then create a new buffer by appending all these sub buffers. I have created a custom buffer builder which will build a node.js buffer by appending strings to it. Following is the new method I have tried, and it worked for me.

var CustomBufferBuilder = function() {

    this.parts = [];
    this.totalLength = 0;

}

CustomBufferBuilder.prototype.append = function( part ) {

    var tempBuffer = Buffer.from( part );
    this.parts.push( tempBuffer );
    this.totalLength += tempBuffer.length;
    this.buffer = undefined;

};

CustomBufferBuilder.prototype.getBuffer = function() {

    if ( !this.buffer ) {

       this.buffer = Buffer.concat( this.parts, this.totalLength );

    }
    return this.buffer;

};


var customBufferBuilder = new CustomBufferBuilder();
var stringsArray = [ 'hello ', 'world.', '\nThis ', 'is', ' a ', 'test.' ];//stringsArray is an array of strings
var len = stringsArray.length;
for( var i = 0; i< len; i++ ){

    customBufferBuilder.append( stringsArray[ i ] );

}

var bufferContent = customBufferBuilder.getBuffer();

var zip = new JSZip();
zip.file( 'test.txt', bufferContent, { binary : true } );
zip.generateAsync( { type : "nodebuffer", pression: 'DEFLATE' } )
.then( function callback( buffer ) {

    fs.writeFile( 'test.zip', buffer, function( err ){

        if( err ){

            //tasks to perform in case of error

        }
        else{

            //other logic

        }

    } );

}, function( e ) {

    //tasks to perform in case of error

} );

As output, I got the zip file( test.zip ) and test.txt inside it. The test.txt file inside the zip file contains the following words, 'hello world.\nThis is a test.'.

Thanks @BrahmaDev for spending time to look into my question :)

发布评论

评论列表(0)

  1. 暂无评论