我正在使用nodejs来构建一个应用程序,该应用程序使用一些外部API,它返回以deflate格式压缩的json数据,但由于我是nodejs的新手,所以我查看了zlib api但是没有找到解码字符串的方法, 请任何人帮助我,这是我的解码字符串:
u�O��0\u0010ſJ�CN4��?RT���ޫʚ�\u0001����f)]��wذڪ�r��7�͓�W�\u0003���?_�\u0005T����\tT����\u000f����y=��cu\u0012��\u000e����A��,5�\u0005\u0017ER�\u0011�^�8\rvE��0x��\u0000>��*�jTRA�\\SQe)/�d�����C�<?�4��4\u0007\b�\u001a��`(��@Y��\u0011{���-j���ք�\u0013� ^Y��T��\u001f��\u0010y�\u001a���=v�\'"�ʒGl��dX\'*��ӞR�b;��Y^���\u0017O{��\u001b��\u000fa��\u0018\u0014��8")\u0006m�\u001f�\u0007h����\u000ev96v�7G\u001f�vw��\t��J�i�U\u001a\u0018�(�7����Nζz@�G�p��iȲ,���\u000b\u0004po3��\u0018���\'i�`�(�S�]��P�Eˋ\u0013���\\�\u0007u�\nMЍ5\u0007w~�qhϜ�~E�\u0007OKp\u001f���l\u0003�\u0011~o\u000b����p\u0004m��X]�Uu�\u000b
答案 0 :(得分:1)
不使用任何第三方库,也不试图检测服务器的编码:
void MainWindow::createPushButtons()
{
QWidget * wdg = new QWidget(this);//--> widget contains pushButtons
wdg->setObjectName("buttonWidget");//--> I set object name in order to catch widget and pushButtons in PaintEvent()
setCentralWidget(wdg);//--> I add widget to app main layout
for(int i=0; i<colors.size(); i++)//--> Button count and color count must be same.
{
QPushButton *btn = new QPushButton(QString::number(i)+". button",wdg);//--> create pushButtons in parent wdg
btn->setGeometry(firstButtonX + (buttonWidth+triangleWidth)*i, firstButtonY, buttonWidth, buttonHeight);//--> I set geometry for pushButtons
btn->setStyleSheet(QString("background-color: %1;border-style: outset;border-width: 0px;").arg(colors.at(i).name()));//--> I change background color and clear border
}
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QWidget *wdg = findChild<QWidget *>("buttonWidget");//--> I catch my widget
QList<QPushButton *> buttons = wdg->findChildren < QPushButton *>();//--> I find pushButtons
for(int i=0;i < buttons.size(); i++)
{
int x = buttons.at(i)->pos().x();//--> measurements for triangle
int y = buttons.at(i)->pos().y();
int w = buttons.at(i)->width();
int h = buttons.at(i)->height();
QRect r(x+w,y,triangleWidth,h);//--> I create rectangular area between pushButtons
QPainter painter(this);
qreal point3X = x+w + triangleWidth;//--> triangle corners
qreal point3Y = y + h/2 ;
qreal point1X = x+w;
qreal point1Y = y;
qreal point2X = x+w;
qreal point2Y = y+h;
QPainterPath path;
path.moveTo (point1X, point1Y);
path.lineTo (point2X, point2Y);
path.lineTo (point3X, point3Y);
painter.setPen (Qt :: NoPen);
if(i != buttons.size()-1)//--> I paint rectangular and triangle
{
painter.fillRect(r, QBrush (colors.at(i+1)));
}
painter.fillPath (path, QBrush (colors.at(i)));
}
}
标准zlib
库也提供其他流处理程序,例如var options = {
hostname: 'www.example.com',
port: 80,
path: '/deflateable-thing',
method: 'GET'
};
var req = http.request(options, function(res) {
deflate = zlib.createDeflate();
res.pipe(deflate);
deflate.on('data', function (chunk) {
console.log('data: ' + chunk.toString());
});
});
req.end();
。
答案 1 :(得分:0)
嗯......首先你需要知道...... API是否产生Raw-Deflate或Deflate。此外,在放气时可以做出许多不同的选择。您必须了解Deflation时使用的配置。我假设您应该能够在您正在使用的API文档中找到它们。
让我们假设API现在正在使用默认设置(https://nodejs.org/api/zlib.html#zlib_options)生成Deflate。
现在...
var zlib = require( 'zlib' );
// lets assume you have your response from api in a string variable
var responseDeflateString = "response from api";
// convert it to a buffer ( as you provided, its a utf8 string)
var responseDeflateBuffer = new Buffer( responseDeflateString );
// now inflate the buffer
// Note 1 : zlib.inflateSync takes another argument "option" and this will
// depend upon how this API service was deflating, and should be added
// based on API documentation. For now we are using default options.
// Note 2 : its better to use the asynchronous version. But we are using
// synchronous one for the sake of simplicity.
var responseInflateBuffer = zlib.inflateSync( responseDeflateBuffer );
// decode the buffer back to string ( utf8 )
var responseInflateString = responseInflateBuffer.toString();
答案 2 :(得分:0)
var request = require('request')
request(
{ method: 'GET'
, uri: 'http://www.google.com'
, gzip: true
}
, function (error, response, body) {
// body is the decompressed response body
console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
console.log('the decoded data is: ' + body)
}
)
.on('data', function(data) {
// decompressed data as it is received
console.log('decoded chunk: ' + data)
})
.on('response', function(response) {
// unmodified http.IncomingMessage object
response.on('data', function(data) {
// compressed data as it is received
console.log('received ' + data.length + ' bytes of compressed data')
})
})