22#include <wx/filedlg.h>
25#include <wx/wfstream.h>
26#include <wx/zipstrm.h>
39#define ZipFileExtension wxT( "zip" )
46 const wxString& aZipFileB,
REPORTER& aReporter )
48 wxFFileInputStream streamA( aZipFileA );
49 wxFFileInputStream streamB( aZipFileB );
51 if( !streamA.IsOk() || !streamB.IsOk() )
57 wxZipInputStream zipStreamA = wxZipInputStream( streamA );
58 wxZipInputStream zipStreamB = wxZipInputStream( streamB );
60 std::set<wxUint32> crcsA;
61 std::set<wxUint32> crcsB;
64 for( wxZipEntry* entry = zipStreamA.GetNextEntry(); entry; entry = zipStreamA.GetNextEntry() )
66 crcsA.insert( entry->GetCrc() );
69 for( wxZipEntry* entry = zipStreamB.GetNextEntry(); entry; entry = zipStreamB.GetNextEntry() )
71 crcsB.insert( entry->GetCrc() );
74 return crcsA == crcsB;
82 wxFFileInputStream stream( aSrcFile );
90 const wxArchiveClassFactory* archiveClassFactory =
91 wxArchiveClassFactory::Find( aSrcFile, wxSTREAM_FILEEXT );
93 if( !archiveClassFactory )
99 std::unique_ptr<wxArchiveInputStream> archiveStream( archiveClassFactory->NewStream( stream ) );
103 for( wxArchiveEntry* entry = archiveStream->GetNextEntry(); entry;
104 entry = archiveStream->GetNextEntry() )
106 fileStatus.Printf(
_(
"Extracting file '%s'." ), entry->GetName() );
109 wxString fullname = aDestDir + entry->GetName();
112 wxString t_path = wxPathOnly( fullname );
114 if( !wxDirExists( t_path ) )
116 wxFileName::Mkdir( t_path, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
124 wxTempFileOutputStream outputFileStream( fullname );
126 if(
CopyStreamData( *archiveStream, outputFileStream, entry->GetSize() ) )
127 outputFileStream.Commit();
132 wxFileName outputFileName( fullname );
133 wxDateTime fileTime = entry->GetDateTime();
136 outputFileName.SetTimes( &fileTime, &fileTime, &fileTime );
145 REPORTER& aReporter,
bool aVerbose,
bool aIncludeExtraFiles )
149 wxT(
"*.kicad_pro" ),
150 wxT(
"*.kicad_prl" ),
151 wxT(
"*.kicad_sch" ),
152 wxT(
"*.kicad_sym" ),
153 wxT(
"*.kicad_pcb" ),
154 wxT(
"*.kicad_mod" ),
155 wxT(
"*.kicad_dru" ),
156 wxT(
"*.kicad_wks" ),
158 wxT(
"fp-lib-table" ),
159 wxT(
"sym-lib-table" )
163 static const wxChar* extraExtensionList[] = {
166 wxT(
"*.lib" ), wxT(
"*.dcm" ),
170 wxT(
"*.stp" ), wxT(
"*.step" ),
172 wxT(
"*.g?" ), wxT(
"*.g??" ),
176 wxT(
"*.drl" ), wxT(
"*.nc" ), wxT(
"*.xnc" ),
183 wxT(
"*.cir" ), wxT(
"*.sub" ), wxT(
"*.model" ),
189 wxString oldCwd = wxGetCwd();
191 wxSetWorkingDirectory( aSrcDir );
193 wxFFileOutputStream ostream( aDestFile );
195 if( !ostream.IsOk() )
197 msg.Printf(
_(
"Failed to create file '%s'." ), aDestFile );
202 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
205 wxString currFilename;
212 if( aIncludeExtraFiles )
214 for(
unsigned ii = 0; ii <
arrayDim( extraExtensionList ); ii++ )
215 wxDir::GetAllFiles( aSrcDir, &files, extraExtensionList[ii] );
218 for(
unsigned ii = 0; ii < files.GetCount(); ++ii )
220 if( files[ii].EndsWith( wxS(
".ibs" ) ) )
222 wxFileName package( files[ ii ] );
223 package.MakeRelativeTo( aSrcDir );
224 package.SetExt( wxS(
"pkg" ) );
226 if( package.Exists() )
227 files.push_back( package.GetFullName() );
233 unsigned long uncompressedBytes = 0;
238 wxString lastStoredFile;
240 for(
unsigned ii = 0; ii < files.GetCount(); ii++ )
242 if( lastStoredFile == files[ii] )
245 lastStoredFile = files[ii];
249 wxFileName curr_fn( files[ii] );
250 curr_fn.MakeRelativeTo( aSrcDir );
251 currFilename = curr_fn.GetFullPath();
254 wxFSFile* infile = fsfile.OpenFile( wxFileSystem::FileNameToURL( curr_fn ) );
258 zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
259 infile->GetStream()->Read( zipstream );
260 zipstream.CloseEntry();
262 uncompressedBytes += infile->GetStream()->GetSize();
266 msg.Printf(
_(
"Archived file '%s'." ), currFilename );
276 msg.Printf(
_(
"Failed to archive file '%s'." ), currFilename );
285 [](
unsigned long aSize ) -> wxString
287 constexpr float KB = 1024.0;
288 constexpr float MB = KB * 1024.0;
291 return wxString::Format( wxT(
"%0.2f MB" ), aSize / MB );
292 else if( aSize >= KB )
293 return wxString::Format( wxT(
"%0.2f KB" ), aSize / KB );
295 return wxString::Format( wxT(
"%lu bytes" ), aSize );
298 size_t zipBytesCnt = ostream.GetSize();
300 if( zipstream.Close() )
302 msg.Printf(
_(
"Zip archive '%s' created (%s uncompressed, %s compressed)." ),
304 reportSize( uncompressedBytes ),
305 reportSize( zipBytesCnt ) );
310 msg.Printf( wxT(
"Failed to create file '%s'." ), aDestFile );
315 wxSetWorkingDirectory( oldCwd );
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
static bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Creates an archive of the project.
static bool Unarchive(const wxString &aSrcFile, const wxString &aDestDir, REPORTER &aReporter)
Extracts an archive of the current project over existing files Warning: this will overwrite files in ...
static bool AreZipArchivesIdentical(const wxString &aZipFileA, const wxString &aZipFileB, REPORTER &aReporter)
Compares 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)=0
Report a string with a given severity.
This file contains miscellaneous commonly used macros and functions.
static const std::vector< std::string > extensionList
Definition of file extensions used in Kicad.
static bool CopyStreamData(wxInputStream &inputStream, wxOutputStream &outputStream, wxFileOffset size)