File: ZipUnpacker.cpp

package info (click to toggle)
fatrat-unpack 1.1.2-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 1,104 kB
  • ctags: 2,579
  • sloc: cpp: 18,714; makefile: 6
file content (188 lines) | stat: -rw-r--r-- 4,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
FatRat download manager
http://fatrat.dolezel.info

Copyright (C) 2006-2009 Lubos Dolezel <lubos a dolezel.info>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "ZipUnpacker.h"
#include <QMessageBox>
#include <fatrat/fatrat.h>
#include <errno.h>
#include <QFile>

ZipUnpacker::ZipUnpacker(QString file, QString transferComment)
	: Unpacker(file), m_strTransferComment(transferComment), m_zip(0), m_nTotal(0)
{
	processArchive();
}

ZipUnpacker::~ZipUnpacker()
{
	if(m_zip)
		zip_close(m_zip);
}

bool ZipUnpacker::supported(QString file)
{
	return true;
}

QString ZipUnpacker::zipError(int code, int scode)
{
	char buffer[255];
	zip_error_to_str(buffer, sizeof(buffer), code, scode);
	return QString::fromUtf8(buffer);
}

void ZipUnpacker::processArchive()
{
	int err;
	QByteArray ba = m_strFile.toUtf8();
	
	try
	{
		m_zip = zip_open(ba.constData(), 0, &err);
		
		if(!m_zip)
			throw zipError(err, errno);
		
		m_strArchiveComment = QString::fromUtf8(zip_get_archive_comment(m_zip, 0, 0));
		
		int nfiles = zip_get_num_files(m_zip);
		
		for(int i=0;i<nfiles;i++)
		{
			FileEntry file;
			struct zip_stat zstat;
			
			if(zip_stat_index(m_zip, i, 0, &zstat) < 0)
			{
				int code, scode;
				zip_error_get(m_zip, &code, &scode);
				throw zipError(code, scode);
			}
			
			file.name = QString::fromUtf8(zstat.name);
			file.size = zstat.size;
			
			m_files << file;
		}
		
		runDialog(m_files);
	}
	catch(QString errString)
	{
		QMessageBox::critical(getMainWindow(), tr("Unpack an archive"), errString);
	}
}

void ZipUnpacker::run()
{
	char buffer[4096];
	qint64 totalDone = 0;
	int totalPercents = 0;
	
	try
	{
		for(int i=0;i<m_files.size();i++)
		{
			if(!m_filesValues[i] || !m_files[i].size)
				continue;
			
			QFile file;
			QString fpath = m_dirDestination.filePath(m_files[i].name);
			struct zip_file* zfile;
			int percents = 0;
			
			zfile = zip_fopen_index(m_zip, i, 0);
			if(!zfile)
			{
				int code, scode;
				zip_error_get(m_zip, &code, &scode);
				throw zipError(code, scode);
			}
			
			try
			{
				file.setFileName(fpath);
				
				int l = m_files[i].name.lastIndexOf('/');
				if(l != -1)
					m_dirDestination.mkpath(m_files[i].name.left(l));
				
				if(!file.open(QIODevice::WriteOnly))
					throw tr("Unable to open %1 for writing.").arg(fpath);
				
				setToolTip(tr("Extracting %1...").arg(m_files[i].name));
				
				for(unsigned int pos=0;pos<m_files[i].size;)
				{
					int read = zip_fread(zfile, buffer, sizeof(buffer));
					if(read < 0)
						throw tr("Read error");
					file.write(buffer, read);
					totalDone += read;
					pos += read;
					
					if(m_nTotal)
					{
						int pcts = 100*totalDone/m_nTotal;
						if(pcts > totalPercents)
							setProgress(totalPercents = pcts);
					}
					if(m_files[i].size)
					{
						int pcts = 100*file.pos()/m_files[i].size;
						if(pcts > percents)
							setFileProgress(i, percents = pcts);
					}
				}
			}
			catch(...)
			{
				zip_fclose(zfile);
				throw;
			}
			zip_fclose(zfile);
		}
	}
	catch(QString errString)
	{
		QMessageBox::critical(getMainWindow(), tr("Unpack an archive"), errString);
	}
	
	connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}

void ZipUnpacker::extract(QList<bool> files, QString where)
{
	m_filesValues = files;
	m_dirDestination = QDir(where);
	
	for(int i=0;i<m_files.size();i++)
	{
		if(m_filesValues[i])
			m_nTotal += m_files[i].size;
	}
	
	start();
}

void ZipUnpacker::pipe(int fileIndex, QProcess* process)
{
}