Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathziphandler.cpp
More file actions
Latest commit
367 lines (343 loc) · 10.2 KB
/
Copy pathziphandler.cpp
File metadata and controls
367 lines (343 loc) · 10.2 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include"ziphandler.h"
#include<QDebug>
#include<QTemporaryDir>
#include<fstream>
#include<QFile>
#include<QFileInfo>
#include<QProcess>
#include<QDirIterator>
#if defined(__GNUC__)
#ifndef _FILE_OFFSET_BITS
#define_FILE_OFFSET_BITS64
#endif
#ifndef _LARGEFILE64_SOURCE
#define_LARGEFILE64_SOURCE1
#endif
#endif
voidZipHandler::resetVars()
{
m_zipFileValid = false;
m_compressionSupportedAudio = false;
m_compressionSupportedCdg = false;
m_audioFileFound = false;
m_cdgFileFound = false;
m_audioSize = 0;
m_cdgSize = 0;
m_compressionLevel = 9;
m_compressFilesWithZipFn = true;
m_rgMarkerFileFound = false;
}
boolZipHandler::extractUsing7z(QString fileName, QString destDir)
{
QTemporaryDir tmpDir;
QString tmpZipPath = tmpDir.path() + QDir::separator() + "tmp.zip";
if (!QFile::copy(fileName, tmpZipPath))
{
qWarning() << "error copying zip";
returnfalse;
}
QProcess *process = newQProcess(this);
QString ext7zPath = "/bin/7za";
QStringList arguments;
arguments << "e";
arguments << "-y";
arguments << "-o" + destDir;
arguments << tmpZipPath;
process->start(settings->get7zipPath(), arguments);
process->waitForFinished();
qWarning() << process->readAllStandardOutput();
qWarning() << process->readAllStandardError();
if (process->exitCode() != 0)
returnfalse;
else
returntrue;
}
boolZipHandler::reZipUnsupported(QString filePath)
{
if (qzFile->isOpen())
{
qWarning() << "Releasing file handle on original zip";
qzFile->close();
}
QString zipFileName = QFileInfo(filePath).fileName();
QTemporaryDir tmpDir;
if (!extractUsing7z(filePath, tmpDir.path()))
{
qWarning() << "Failed to extract files";
returnfalse;
}
QString audioFile;
QString cdgFile;
QDirIterator iterator(tmpDir.path(), QDirIterator::Subdirectories);
while (iterator.hasNext()) {
iterator.next();
if (!iterator.fileInfo().isDir()) {
QString fn = iterator.filePath();
if (fn.endsWith(".cdg", Qt::CaseInsensitive))
{
cdgFile = fn;
}
else
{
for (int e=0; e < audioExtensions.size(); e++)
{
if (fn.endsWith(audioExtensions.at(e), Qt::CaseInsensitive))
{
audioFile = fn;
}
}
}
}
}
if (audioFile == "" || cdgFile == "")
{
qWarning() << "File missing either mp3 of cdg file, bailing out";
returnfalse;
}
createZip(tmpDir.path() + QDir::separator() + zipFileName, cdgFile, audioFile);
QString tmpFilePath = QString(filePath + ".tmp");
if (!QFile::rename(filePath, tmpFilePath))
{
qWarning() << "Unable to move original to temporary file, bailing out";
qWarning() << "rename(" << filePath << ", " << tmpFilePath << ")";
returnfalse;
}
if (!QFile::copy(tmpDir.path() + QDir::separator() + zipFileName, filePath))
{
qWarning() << "Unable to move new file into place, moving original back and bailing out";
QFile::rename(filePath + ".tmp", filePath);
returnfalse;
}
if (QFile::exists(filePath))
{
QFile::remove(tmpFilePath);
returntrue;
}
returnfalse;
}
QString ZipHandler::getAudioExt()
{
return m_audioExt;
}
QStringList ZipHandler::getSupportedAudioExts()
{
return audioExtensions;
}
voidZipHandler::close()
{
if (qzFile->isOpen())
qzFile->close();
}
voidZipHandler::loadExtensions()
{
audioExtensions.append(".mp3");
audioExtensions.append(".wav");
audioExtensions.append(".ogg");
audioExtensions.append(".mov");
audioExtensions.append(".flac");
}
voidZipHandler::initializeZipFile()
{
mz_zip_reader_end(m_archive);
qzFile->close();
qzFile->setFileName(m_zipFile);
qzFile->open(QFile::ReadOnly);
int fileHandle = qzFile->handle();
czFile = fdopen(fileHandle, "rb");
mz_zip_archive_file_stat fStat;
if (!mz_zip_reader_init_cfile(m_archive,czFile,qzFile->size(),0))
{
m_zipFileValid = false;
qWarning() << "Error opening zip file";
return;
}
m_zipFileValid = true;
unsignedint files = mz_zip_reader_get_num_files(m_archive);
for (unsignedint i=0; i < files; i++)
{
if (mz_zip_reader_file_stat(m_archive, i, &fStat))
{
QString fileName = fStat.m_filename;
if (fileName == "ReplayGainProcessed")
{
m_rgMarkerFileFound = true;
}
elseif (fileName.endsWith(".cdg",Qt::CaseInsensitive))
{
m_cdgFileName = fileName;
m_cdgSize = fStat.m_uncomp_size;
m_compressionSupportedCdg = fStat.m_is_supported;
m_cdgFileFound = true;
}
else
{
for (int e=0; e < audioExtensions.size(); e++)
{
if (fileName.endsWith(audioExtensions.at(e), Qt::CaseInsensitive))
{
m_audioFileName = fileName;
m_audioExt = audioExtensions.at(e);
m_audioSize = fStat.m_uncomp_size;
m_compressionSupportedAudio = fStat.m_is_supported;
m_audioFileFound = true;
}
}
}
}
else
{
m_zipFileValid = false;
return;
}
}
}
ZipHandler::ZipHandler(QString zipFile, QObject *parent) :
QObject(parent)
{
settings = newSettings(this);
m_archive = new mz_zip_archive;
mz_zip_zero_struct(m_archive);
qzFile = newQFile(this);
loadExtensions();
m_zipFile = zipFile;
resetVars();
initializeZipFile();
}
ZipHandler::ZipHandler(QObject *parent) :
QObject(parent)
{
settings = newSettings(this);
m_archive = new mz_zip_archive;
mz_zip_zero_struct(m_archive);
qzFile = newQFile(this);
loadExtensions();
m_zipFile = "";
resetVars();
}
boolZipHandler::extractAudio(QDir destDir)
{
if (m_audioFileFound && m_compressionSupportedAudio)
{
QString outFile(destDir.path() + QDir::separator() + "tmp" + m_audioExt);
if (mz_zip_reader_extract_file_to_file(m_archive, m_audioFileName.toLocal8Bit().data(),outFile.toLocal8Bit().data(),0))
{
returntrue;
}
}
returnfalse;
}
boolZipHandler::extractCdg(QDir destDir)
{
if (m_cdgFileFound && m_compressionSupportedCdg)
{
QString outFile(destDir.path() + QDir::separator() + "tmp.cdg");
if (mz_zip_reader_extract_file_to_file(m_archive, m_cdgFileName.toLocal8Bit().data(),outFile.toLocal8Bit().data(),0))
{
returntrue;
}
}
returnfalse;
}
QString ZipHandler::zipFile() const
{
return m_zipFile;
}
voidZipHandler::setZipFile(const QString &zipFile)
{
m_zipFile = zipFile;
resetVars();
initializeZipFile();
}
intZipHandler::getSongDuration()
{
return ((m_cdgSize / 96) / 75) * 1000;
}
boolZipHandler::createZip(QString zipFile, QString cdgFile, QString audioFile, bool addRgMarker)
{
QString audioFileName, cdgFileName;
if (m_compressFilesWithZipFn)
{
QString zipFileBaseName, audioFileExt;
zipFileBaseName = QFileInfo(zipFile).completeBaseName();
audioFileExt = QFileInfo(audioFile).suffix();
cdgFileName = zipFileBaseName + ".cdg";
audioFileName = zipFileBaseName + "." + audioFileExt;
}
else
{
audioFileName = QFileInfo(audioFile).fileName();
cdgFileName = QFileInfo(cdgFile).fileName();
}
qWarning() << "Creating zip file: " << zipFile;
qWarning() << "Adding: " << cdgFile << " as " << cdgFileName;
qWarning() << "Adding: " << audioFile << " as " << audioFileName;
mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_writer_init_file(&zip_archive, zipFile.toLocal8Bit().data(),0))
{
qWarning() << "Error initializing zip file";
}
constchar *s_pComment = "File has been processed through ReplayGain with OpenKJ KaraokeRG";
qWarning() << "ZipHandler::createZip(" << zipFile << ", " << cdgFile << ", " << audioFile << ")";
qWarning() << "Adding mp3 file";
if (!mz_zip_writer_add_file(&zip_archive, audioFileName.toLocal8Bit().data(), audioFile.toLocal8Bit().data(), NULL, 0, m_compressionLevel))
{
qWarning() << "Error adding mp3 file to archive";
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
returnfalse;
}
qWarning() << "Adding cdg file";
if (!mz_zip_writer_add_file(&zip_archive, cdgFileName.toLocal8Bit().data(), cdgFile.toLocal8Bit().data(), NULL, 0, m_compressionLevel))
{
qWarning() << "Error adding cdg file to archive";
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
returnfalse;
}
if (addRgMarker)
{
qWarning() << "Adding marker to file";
if (!mz_zip_writer_add_mem(&zip_archive,"ReplayGainProcessed", s_pComment, sizeof(s_pComment), m_compressionLevel))
{
qWarning() << "Error adding marker file to archive";
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
returnfalse;
}
}
qWarning() << "Files added to archive";
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
returntrue;
}
boolZipHandler::isValidZipfile()
{
return m_zipFileValid;
}
boolZipHandler::containsRGMarkerFile()
{
return m_rgMarkerFileFound;
}
boolZipHandler::compressionSupported()
{
if (m_compressionSupportedAudio && m_compressionSupportedCdg)
returntrue;
returnfalse;
}
boolZipHandler::containsAudioFile()
{
return m_audioFileFound;
}
boolZipHandler::containsCdgFile()
{
return m_cdgFileFound;
}
voidZipHandler::setCompressionLevel(int level)
{
m_compressionLevel = level;
}
voidZipHandler::setReplaceFnsWithZipFn(bool replace)
{
m_compressFilesWithZipFn = replace;
}