22#include <wx/filedlg.h>
25#include <wx/wfstream.h>
26#include <wx/zipstrm.h>
37#define ZipFileExtension wxT( "zip" )
49 wxFFileInputStream stream( aSrcFile );
57 const wxArchiveClassFactory* archiveClassFactory =
58 wxArchiveClassFactory::Find( aSrcFile, wxSTREAM_FILEEXT );
60 if( !archiveClassFactory )
66 std::unique_ptr<wxArchiveInputStream> archiveStream( archiveClassFactory->NewStream( stream ) );
70 for( wxArchiveEntry* entry = archiveStream->GetNextEntry(); entry;
71 entry = archiveStream->GetNextEntry() )
73 fileStatus.Printf(
_(
"Extracting file '%s'." ), entry->GetName() );
76 wxString fullname = aDestDir + entry->GetName();
79 wxString t_path = wxPathOnly( fullname );
81 if( !wxDirExists( t_path ) )
83 wxFileName::Mkdir( t_path, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
91 wxTempFileOutputStream outputFileStream( fullname );
93 if(
CopyStreamData( *archiveStream, outputFileStream, entry->GetSize() ) )
94 outputFileStream.Commit();
99 wxFileName outputFileName( fullname );
100 wxDateTime fileTime = entry->GetDateTime();
103 outputFileName.SetTimes( &fileTime, &fileTime, &fileTime );
112 REPORTER& aReporter,
bool aVerbose,
bool aIncludeExtraFiles )
116 wxT(
"*.kicad_pro" ),
117 wxT(
"*.kicad_prl" ),
118 wxT(
"*.kicad_sch" ),
119 wxT(
"*.kicad_sym" ),
120 wxT(
"*.kicad_pcb" ),
121 wxT(
"*.kicad_mod" ),
122 wxT(
"*.kicad_dru" ),
123 wxT(
"*.kicad_wks" ),
125 wxT(
"fp-lib-table" ),
126 wxT(
"sym-lib-table" )
130 static const wxChar* extraExtensionList[] = {
133 wxT(
"*.lib" ), wxT(
"*.dcm" ),
137 wxT(
"*.stp" ), wxT(
"*.step" ),
139 wxT(
"*.g?" ), wxT(
"*.g??" ),
143 wxT(
"*.drl" ), wxT(
"*.nc" ), wxT(
"*.xnc" ),
150 wxT(
"*.cir" ), wxT(
"*.sub" ), wxT(
"*.model" ),
156 wxString oldCwd = wxGetCwd();
158 wxSetWorkingDirectory( aSrcDir );
160 wxFFileOutputStream ostream( aDestFile );
162 if( !ostream.IsOk() )
164 msg.Printf(
_(
"Failed to create file '%s'." ), aDestFile );
169 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
172 wxString currFilename;
179 if( aIncludeExtraFiles )
181 for(
unsigned ii = 0; ii <
arrayDim( extraExtensionList ); ii++ )
182 wxDir::GetAllFiles( aSrcDir, &files, extraExtensionList[ii] );
185 for(
unsigned ii = 0; ii < files.GetCount(); ++ii )
187 if( files[ii].EndsWith( wxS(
".ibs" ) ) )
189 wxFileName package( files[ ii ] );
190 package.MakeRelativeTo( aSrcDir );
191 package.SetExt( wxS(
"pkg" ) );
193 if( package.Exists() )
194 files.push_back( package.GetFullName() );
200 unsigned long uncompressedBytes = 0;
205 wxString lastStoredFile;
207 for(
unsigned ii = 0; ii < files.GetCount(); ii++ )
209 if( lastStoredFile == files[ii] )
212 lastStoredFile = files[ii];
216 wxFileName curr_fn( files[ii] );
217 curr_fn.MakeRelativeTo( aSrcDir );
218 currFilename = curr_fn.GetFullPath();
221 wxFSFile* infile = fsfile.OpenFile( wxFileSystem::FileNameToURL( curr_fn ) );
225 zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
226 infile->GetStream()->Read( zipstream );
227 zipstream.CloseEntry();
229 uncompressedBytes += infile->GetStream()->GetSize();
233 msg.Printf(
_(
"Archived file '%s'." ), currFilename );
243 msg.Printf(
_(
"Failed to archive file '%s'." ), currFilename );
252 [](
unsigned long aSize ) -> wxString
254 constexpr float KB = 1024.0;
255 constexpr float MB = KB * 1024.0;
258 return wxString::Format( wxT(
"%0.2f MB" ), aSize / MB );
259 else if( aSize >= KB )
260 return wxString::Format( wxT(
"%0.2f KB" ), aSize / KB );
262 return wxString::Format( wxT(
"%lu bytes" ), aSize );
265 size_t zipBytesCnt = ostream.GetSize();
267 if( zipstream.Close() )
269 msg.Printf(
_(
"Zip archive '%s' created (%s uncompressed, %s compressed)." ),
271 reportSize( uncompressedBytes ),
272 reportSize( zipBytesCnt ) );
277 msg.Printf( wxT(
"Failed to create file '%s'." ), aDestFile );
282 wxSetWorkingDirectory( oldCwd );
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Creates an archive of the project.
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 ...
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)