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
|
/////////////////////////////////////////////////////////////////////////////
// Name: samples/archive/archive.cpp
// Purpose: A sample application to manipulate archives
// Author: Tobias Taschner
// Created: 2018-02-07
// Copyright: (c) 2018 wxWidgets development team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/archive.h"
#include "wx/app.h"
#include "wx/cmdline.h"
#include "wx/filename.h"
#include "wx/scopedptr.h"
#include "wx/vector.h"
#include "wx/wfstream.h"
#include "wx/zipstrm.h"
class ArchiveApp: public wxAppConsole
{
public:
ArchiveApp()
{
m_forceZip64 = false;
m_archiveClassFactory = NULL;
m_filterClassFactory = NULL;
}
virtual void OnInitCmdLine(wxCmdLineParser& parser) wxOVERRIDE;
virtual bool OnCmdLineParsed(wxCmdLineParser& parser) wxOVERRIDE;
virtual int OnRun() wxOVERRIDE;
private:
enum ArchiveCommandType
{
CMD_CREATE,
CMD_LIST,
CMD_EXTRACT,
CMD_NONE
};
struct ArchiveCommandDesc
{
ArchiveCommandType type;
const char* id;
const char* desc;
};
static const ArchiveCommandDesc s_cmdDesc[];
ArchiveCommandType m_command;
wxString m_archiveFileName;
wxVector<wxString> m_fileNames;
bool m_forceZip64;
// At most one of these pointers is non-NULL.
const wxArchiveClassFactory* m_archiveClassFactory;
const wxFilterClassFactory* m_filterClassFactory;
int DoCreate();
int DoList();
int DoExtract();
bool CopyStreamData(wxInputStream& inputStream, wxOutputStream& outputStream, wxFileOffset size);
};
const ArchiveApp::ArchiveCommandDesc ArchiveApp::s_cmdDesc[] =
{
{CMD_CREATE, "c", "create"},
{CMD_LIST, "l", "list"},
{CMD_EXTRACT, "x", "extract"},
{CMD_NONE, 0, 0}
};
// Helper function iterating over all factories of the given type (assumed to
// derive from wxFilterClassFactoryBase) and returning a string containing all
// comma-separated values returned from their GetProtocols() called with the
// given protocol type.
template <typename T>
void AppendAllSupportedOfType(wxString& all, wxStreamProtocolType type)
{
for (const T* factory = T::GetFirst(); factory; factory = factory->GetNext())
{
const wxChar* const* exts = factory->GetProtocols(type);
while (*exts)
{
if (!all.empty())
all+= ", ";
all += *exts++;
}
}
}
// Returns all supported protocols or extensions (depending on the type
// parameter value) for both archive and filter factories.
wxString GetAllSupported(wxStreamProtocolType type)
{
wxString all;
AppendAllSupportedOfType<wxArchiveClassFactory>(all, type);
AppendAllSupportedOfType<wxFilterClassFactory>(all, type);
return all;
}
void ArchiveApp::OnInitCmdLine(wxCmdLineParser& parser)
{
wxAppConsole::OnInitCmdLine(parser);
parser.AddParam("command");
parser.AddParam("archive_name");
parser.AddParam("file_names", wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL);
parser.AddSwitch("", "force-zip64", "Force ZIP64 format when creating ZIP archives");
parser.AddUsageText("\ncommand:");
for (const ArchiveCommandDesc* cmdDesc = s_cmdDesc; cmdDesc->type != CMD_NONE; cmdDesc++)
parser.AddUsageText(wxString::Format(" %s: %s", cmdDesc->id, cmdDesc->desc));
parser.AddUsageText("\nsupported formats: " + GetAllSupported(wxSTREAM_PROTOCOL));
}
bool ArchiveApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
m_command = CMD_NONE;
wxString command = parser.GetParam();
for (const ArchiveCommandDesc* cmdDesc = s_cmdDesc; cmdDesc->type != CMD_NONE; cmdDesc++)
{
if (cmdDesc->id == command)
m_command = cmdDesc->type;
}
if(m_command == CMD_NONE)
{
wxLogError("Invalid command: %s", command);
return false;
}
m_archiveFileName = parser.GetParam(1);
for (size_t i = 2; i < parser.GetParamCount(); i++)
m_fileNames.push_back(parser.GetParam(i));
m_forceZip64 = parser.FoundSwitch("force-zip64") == wxCMD_SWITCH_ON;
return wxAppConsole::OnCmdLineParsed(parser);
}
bool ArchiveApp::CopyStreamData(wxInputStream& inputStream, wxOutputStream& outputStream, wxFileOffset size)
{
wxChar buf[128 * 1024];
int readSize = 128 * 1024;
wxFileOffset copiedData = 0;
for (;;)
{
if (size != -1 && copiedData + readSize > size)
readSize = size - copiedData;
inputStream.Read(buf, readSize);
size_t actuallyRead = inputStream.LastRead();
outputStream.Write(buf, actuallyRead);
if (outputStream.LastWrite() != actuallyRead)
{
wxLogError("Failed to output data");
return false;
}
if (size == -1)
{
if (inputStream.Eof())
break;
}
else
{
copiedData += actuallyRead;
if (copiedData >= size)
break;
}
}
return true;
}
int ArchiveApp::DoCreate()
{
if (m_fileNames.empty())
{
wxLogError("Need at least one file to add to archive");
return -1;
}
wxTempFileOutputStream fileOutputStream(m_archiveFileName);
if (m_archiveClassFactory)
{
wxScopedPtr<wxArchiveOutputStream> archiveOutputStream(m_archiveClassFactory->NewStream(fileOutputStream));
if (m_archiveClassFactory->GetProtocol().IsSameAs("zip", false) && m_forceZip64)
reinterpret_cast<wxZipOutputStream*>(archiveOutputStream.get())->SetFormat(wxZIP_FORMAT_ZIP64);
for(wxVector<wxString>::iterator fileName = m_fileNames.begin(); fileName != m_fileNames.end(); fileName++)
{
wxFileName inputFileName(*fileName);
wxPrintf("Adding %s...\n", inputFileName.GetFullName());
wxFileInputStream inputFileStream(*fileName);
if (!inputFileStream.IsOk())
{
wxLogError("Could not open file");
return 1;
}
if (!archiveOutputStream->PutNextEntry(inputFileName.GetFullName(), wxDateTime::Now(), inputFileStream.GetLength()))
break;
if (!CopyStreamData(inputFileStream, *archiveOutputStream, inputFileStream.GetLength()))
return 1;
}
if (!archiveOutputStream->Close())
return 1;
}
else // use m_filterClassFactory
{
if (m_fileNames.size() != 1)
{
wxLogError("Filter-based formats only support archives consisting of a single file");
return -1;
}
wxScopedPtr<wxFilterOutputStream> filterOutputStream(m_filterClassFactory->NewStream(fileOutputStream));
wxFileInputStream inputFileStream(*m_fileNames.begin());
if (!inputFileStream.IsOk())
{
wxLogError("Could not open file");
return 1;
}
if (!CopyStreamData(inputFileStream, *filterOutputStream, inputFileStream.GetLength()))
return 1;
}
fileOutputStream.Commit();
wxPrintf("Created archive\n");
return 0;
}
int ArchiveApp::DoList()
{
wxFileInputStream fileInputStream(m_archiveFileName);
if (!fileInputStream.IsOk())
return 1;
if (m_archiveClassFactory)
{
wxScopedPtr<wxArchiveInputStream> archiveStream(m_archiveClassFactory->NewStream(fileInputStream));
wxPrintf("Archive: %s\n", m_archiveFileName);
wxPrintf("Length Date Time Name\n");
wxPrintf("---------- ---------- -------- ----\n");
wxFileOffset combinedSize = 0;
int entryCount = 0;
for (wxArchiveEntry* entry = archiveStream->GetNextEntry(); entry;
entry = archiveStream->GetNextEntry())
{
combinedSize += entry->GetSize();
entryCount++;
wxPrintf("%10lld %s %s %s\n",
entry->GetSize(),
entry->GetDateTime().FormatISODate(),
entry->GetDateTime().FormatISOTime(),
entry->GetName());
}
wxPrintf("---------- -------\n");
wxPrintf("%10lld %d files\n", combinedSize, entryCount);
}
else
{
wxPrintf("Archive \"%s\" contains a single file named \"%s\"\n",
m_archiveFileName, wxFileName(m_archiveFileName).GetName());
}
return 0;
}
int ArchiveApp::DoExtract()
{
wxFileInputStream fileInputStream(m_archiveFileName);
if (!fileInputStream.IsOk())
return 1;
if (m_archiveClassFactory)
{
wxScopedPtr<wxArchiveInputStream> archiveStream(m_archiveClassFactory->NewStream(fileInputStream));
wxPrintf("Extracting from: %s\n", m_archiveFileName);
for (wxArchiveEntry* entry = archiveStream->GetNextEntry(); entry;
entry = archiveStream->GetNextEntry())
{
wxPrintf("Extracting: %s...\n", entry->GetName());
wxTempFileOutputStream outputFileStream(entry->GetName());
if (!CopyStreamData(*archiveStream, outputFileStream, entry->GetSize()))
return 1;
outputFileStream.Commit();
}
wxPrintf("Extracted all files\n");
}
else
{
wxScopedPtr<wxFilterInputStream> filterStream(m_filterClassFactory->NewStream(fileInputStream));
wxPrintf("Extracting single file from: %s\n", m_archiveFileName);
wxTempFileOutputStream outputFileStream(wxFileName(m_archiveFileName).GetName());
if (!CopyStreamData(*filterStream, outputFileStream, -1))
return 1;
outputFileStream.Commit();
wxPrintf("Extracted successfully\n");
}
return 0;
}
int ArchiveApp::OnRun()
{
m_archiveClassFactory = wxArchiveClassFactory::Find(m_archiveFileName, wxSTREAM_FILEEXT);
if (!m_archiveClassFactory)
{
m_filterClassFactory = wxFilterClassFactory::Find(m_archiveFileName, wxSTREAM_FILEEXT);
if (!m_filterClassFactory)
{
wxLogError("File \"%s\" has unsupported extension, supported ones are: %s",
m_archiveFileName, GetAllSupported(wxSTREAM_FILEEXT));
return -1;
}
}
int result = -1;
switch (m_command) {
case CMD_CREATE:
result = DoCreate();
break;
case CMD_LIST:
result = DoList();
break;
case CMD_EXTRACT:
result = DoExtract();
break;
default:
break;
}
return result;
}
wxIMPLEMENT_APP(ArchiveApp);
|