Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Kaz32
Posts: 236
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Telkomsel
Accepted Solution

How to download zip file? And also open / extract it

Hi, I'm has API from webservice that will download a zip file. (If iI try hit this API on browser, zip file will be downloaded)

How to download it and how to open / extract it on blackberry cascades?

 

I'm  trying to write it to file from 

 

reply->readAll() on HttpFinished

 

but not success to write / download it

 

 

Please help

Thanks

Please use plain text.
Developer
Kaz32
Posts: 236
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Telkomsel

Re: How to download zip file? And also open / extract it

Hi, any suggestion?

 

 

Thanks

Please use plain text.
Contributor
chriske86
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: How to download zip file? And also open / extract it

Hi,

 

you have more ways to download a file, I will show one of them.

 

I'm using QHttp to download large files against QNetworkRequest. The API reference tells, that QHttp is old, and if we can, try to avoid it. But QNetworkRequest has no methods to download a remote file directly into a local file. So my method is old, but it works.

 

You need a QFile* and a QHttp* in the header:
private:
QFile* zipFile;
QHttp* zipRequest;


///cpp

void MyApp::getZip()
{

   QUrl* zipUrl = new QUrl("http://www.xxx.com/example.zip");


   zipRequest = new QHttp(this);
   zipRequest->setHost(zipUrl->host());

   zipFile = new QFile("path + example.zip");
   if (!file->open(QIODevice::WriteOnly) )
   {
      //can't open the file for write
   } else {
      //file created and opened

      zipRequest->get(zipUrl->path(), zipFile);
      connect(zipRequest, SIGNAL(done(bool)), this, SLOT(done(bool)));

   }
}

void MyApp::done(bool error)
{
   zipRequest->deleteLater();

   zipFile->close();

   if (error)
   qDebug() << zipRequest->errorString();
}

 That's all.

 

To extract a zip file I'm using QuaZIP.

Here is a link:

http://quazip.sourceforge.net/

 

Hope it helps,

cheers,

chriske

--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Please use plain text.
Developer
Kaz32
Posts: 236
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Telkomsel

Re: How to download zip file? And also open / extract it

Hi chriske,

 

I will try your suggestion.

 

Thanks for your respond.

Please use plain text.
Contributor
chriske86
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: How to download zip file? And also open / extract it

Your welcome.

 

I've forgot, but there are other signals to monitor the download process, for example

void dataReadProgress(int done,int total);

 

You can find them in the API reference of QHttp.

--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Please use plain text.
Developer
Kaz32
Posts: 236
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Telkomsel

Re: How to download zip file? And also open / extract it

I'm using this, and it work

 

void MyClass::downloadZip(const QString &aUrl)
	{
	QUrl *zipUrl = new QUrl(aUrl);
	zipRequest = new QHttp(this);
	zipRequest->setHost(zipUrl->host());
	zipRequest->get(aUrl);
	connect(zipRequest, SIGNAL(requestFinished(int, bool)), this, SLOT(zipFinished(int, bool)));
	}

void MyClass::zipFinished(int aId, bool aError)
	{
	if (aError)
		{
		qDebug() << zipRequest->errorString();
		showToast("Download fail");
		}
	else
		{
		QFile *zipFile = new QFile(QDir::currentPath() + "/app/native/assets/myZip.zip");
		if (zipFile->open(QIODevice::WriteOnly))
			{
			zipFile->write(zipRequest->readAll());
			zipFile->flush();
			zipFile->close();
			}
		delete zipFile;
		}
	}

 

Please use plain text.