22#include <wx/filedlg.h>
25#include <wx/wfstream.h>
26#include <wx/zipstrm.h>
41#define ZipFileExtension wxT( "zip" )
47 REPORTER& aReporter,
bool aVerbose ) :
56 virtual wxDirTraverseResult
OnFile(
const wxString& aFilename )
override
65 wxFileName package( aFilename );
67 package.SetExt( wxS(
"pkg" ) );
69 if( package.Exists() )
74 return wxDIR_CONTINUE;
77 virtual wxDirTraverseResult
OnDir(
const wxString& aDirname )
override
79 return wxDIR_CONTINUE;
98 wxFileName curr_fn( aFilename );
101 wxString currFilename = curr_fn.GetFullPath();
104 wxFSFile* infile = fsfile.OpenFile( currFilename );
108 m_zipFile.PutNextEntry( currFilename, infile->GetModificationTime() );
116 msg.Printf(
_(
"Archived file '%s'." ), currFilename );
126 msg.Printf(
_(
"Failed to archive file '%s'." ), currFilename );
153 const wxString& aZipFileB,
REPORTER& aReporter )
155 wxFFileInputStream streamA( aZipFileA );
156 wxFFileInputStream streamB( aZipFileB );
158 if( !streamA.IsOk() || !streamB.IsOk() )
164 wxZipInputStream zipStreamA = wxZipInputStream( streamA );
165 wxZipInputStream zipStreamB = wxZipInputStream( streamB );
167 std::set<wxUint32> crcsA;
168 std::set<wxUint32> crcsB;
171 for( wxZipEntry* entry = zipStreamA.GetNextEntry(); entry; entry = zipStreamA.GetNextEntry() )
173 crcsA.insert( entry->GetCrc() );
176 for( wxZipEntry* entry = zipStreamB.GetNextEntry(); entry; entry = zipStreamB.GetNextEntry() )
178 crcsB.insert( entry->GetCrc() );
181 return crcsA == crcsB;
189 wxFFileInputStream stream( aSrcFile );
197 const wxArchiveClassFactory* archiveClassFactory =
198 wxArchiveClassFactory::Find( aSrcFile, wxSTREAM_FILEEXT );
200 if( !archiveClassFactory )
206 std::unique_ptr<wxArchiveInputStream> archiveStream( archiveClassFactory->NewStream( stream ) );
210 for( wxArchiveEntry* entry = archiveStream->GetNextEntry(); entry;
211 entry = archiveStream->GetNextEntry() )
213 fileStatus.Printf(
_(
"Extracting file '%s'." ), entry->GetName() );
216 wxString fullname = aDestDir + entry->GetName();
219 wxString t_path = wxPathOnly( fullname );
221 if( !wxDirExists( t_path ) )
223 wxFileName::Mkdir( t_path, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
231 wxTempFileOutputStream outputFileStream( fullname );
233 if(
CopyStreamData( *archiveStream, outputFileStream, entry->GetSize() ) )
234 outputFileStream.Commit();
239 wxFileName outputFileName( fullname );
240 wxDateTime fileTime = entry->GetDateTime();
243 outputFileName.SetTimes( &fileTime, &fileTime, &fileTime );
252 REPORTER& aReporter,
bool aVerbose,
bool aIncludeExtraFiles )
255#define EXT( ext ) "\\." + ext + "|"
256#define NAME( name ) name + "|"
257#define EXT_NO_PIPE( ext ) "\\." + ext
258#define NAME_NO_PIPE( name ) name
261 std::string fileExtensionRegex =
"("
278 if( aIncludeExtraFiles )
280 fileExtensionRegex +=
"|"
309 fileExtensionRegex +=
")";
318 wxString oldCwd = wxGetCwd();
320 wxFileName sourceDir( aSrcDir );
322 wxSetWorkingDirectory( sourceDir.GetFullPath() );
324 wxFFileOutputStream ostream( aDestFile );
326 if( !ostream.IsOk() )
328 msg.Printf(
_(
"Failed to create file '%s'." ), aDestFile );
333 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
335 wxDir projectDir( aSrcDir );
336 wxString currFilename;
339 if( !projectDir.IsOpened() )
343 msg.Printf(
_(
"Error opening directory: '%s'." ), aSrcDir );
347 wxSetWorkingDirectory( oldCwd );
354 aReporter, aVerbose );
356 projectDir.Traverse( traverser );
361 [](
unsigned long aSize ) -> wxString
363 constexpr float KB = 1024.0;
364 constexpr float MB = KB * 1024.0;
367 return wxString::Format( wxT(
"%0.2f MB" ), aSize / MB );
368 else if( aSize >= KB )
369 return wxString::Format( wxT(
"%0.2f KB" ), aSize / KB );
371 return wxString::Format( wxT(
"%lu bytes" ), aSize );
374 size_t zipBytesCnt = ostream.GetSize();
377 if( zipstream.Close() )
379 msg.Printf(
_(
"Zip archive '%s' created (%s uncompressed, %s compressed)." ),
381 reportSize( uncompressedBytes ),
382 reportSize( zipBytesCnt ) );
387 msg.Printf( wxT(
"Failed to create file '%s'." ), aDestFile );
392 catch(
const std::regex_error& e )
395 wxASSERT_MSG(
false, e.what() );
399 msg.Printf(
_(
"Error: '%s'." ), e.what() );
406 wxSetWorkingDirectory( oldCwd );
wxZipOutputStream & m_zipFile
virtual wxDirTraverseResult OnDir(const wxString &aDirname) override
PROJECT_ARCHIVER_DIR_ZIP_TRAVERSER(const std::string &aExtRegex, const wxString &aPrjDir, wxZipOutputStream &aZipFileOutput, REPORTER &aReporter, bool aVerbose)
virtual wxDirTraverseResult OnFile(const wxString &aFilename) override
unsigned long m_uncompressedBytes
void addFileToZip(const wxString &aFilename)
std::regex m_fileExtRegex
unsigned long GetUncompressedBytes() const
bool GetErrorOccurred() const
static bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Create an archive of the project.
static bool Unarchive(const wxString &aSrcFile, const wxString &aDestDir, REPORTER &aReporter)
Extract an archive of the current project over existing files.
static bool AreZipArchivesIdentical(const wxString &aZipFileA, const wxString &aZipFileB, REPORTER &aReporter)
Compare the CRCs of all the files in zip archive to determine whether the archives are identical.
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
static const std::string LegacySchematicFileExtension
static const wxString GerberFileExtensionsRegex
static const std::string NetlistFileExtension
static const std::string SymbolLibraryTableFileName
static const std::string GerberJobFileExtension
static const std::string StepFileAbrvExtension
static const std::string WorkbookFileExtension
static const std::string ReportFileExtension
static const std::string ProjectFileExtension
static const std::string FootprintPlaceFileExtension
static const std::string JsonFileExtension
static const std::string LegacyPcbFileExtension
static const std::string LegacyProjectFileExtension
static const std::string ProjectLocalSettingsFileExtension
static const std::string KiCadSchematicFileExtension
static const std::string LegacySymbolLibFileExtension
static const std::string DesignBlockLibraryTableFileName
static const std::string KiCadSymbolLibFileExtension
static const std::string SpiceFileExtension
static const std::string PdfFileExtension
static const std::string TextFileExtension
static const std::string FootprintLibraryTableFileName
static const std::string DrawingSheetFileExtension
static const std::string IbisFileExtension
static const std::string IpcD356FileExtension
static const std::string KiCadJobSetFileExtension
static const std::string LegacyFootprintLibPathExtension
static const std::string PythonFileExtension
static const std::string StepFileExtension
static const std::string LegacySymbolDocumentFileExtension
static const std::string FootprintAssignmentFileExtension
static const std::string DrillFileExtension
static const std::string SpiceSubcircuitFileExtension
static const std::string SpiceModelFileExtension
static const std::string DesignRulesFileExtension
static const std::string VrmlFileExtension
static const std::string KiCadFootprintFileExtension
static const std::string KiCadPcbFileExtension
This file contains miscellaneous commonly used macros and functions.
#define NAME_NO_PIPE(name)
Definition of file extensions used in Kicad.
static bool CopyStreamData(wxInputStream &inputStream, wxOutputStream &outputStream, wxFileOffset size)