21#include <wx/filedlg.h>
24#include <wx/wfstream.h>
25#include <wx/zipstrm.h>
36#define ZipFileExtension wxT( "zip" )
48 wxFFileInputStream stream( aSrcFile );
56 const wxArchiveClassFactory* archiveClassFactory =
57 wxArchiveClassFactory::Find( aSrcFile, wxSTREAM_FILEEXT );
59 if( !archiveClassFactory )
65 wxScopedPtr<wxArchiveInputStream> archiveStream( archiveClassFactory->NewStream( stream ) );
69 for( wxArchiveEntry* entry = archiveStream->GetNextEntry(); entry;
70 entry = archiveStream->GetNextEntry() )
72 fileStatus.Printf(
_(
"Extracting file '%s'." ), entry->GetName() );
75 wxString fullname = aDestDir + entry->GetName();
78 wxString t_path = wxPathOnly( fullname );
80 if( !wxDirExists( t_path ) )
82 wxFileName::Mkdir( t_path, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
90 wxTempFileOutputStream outputFileStream( fullname );
92 if(
CopyStreamData( *archiveStream, outputFileStream, entry->GetSize() ) )
93 outputFileStream.Commit();
98 wxFileName outputFileName( fullname );
99 wxDateTime fileTime = entry->GetDateTime();
102 outputFileName.SetTimes( &fileTime, &fileTime, &fileTime );
111 REPORTER& aReporter,
bool aVerbose,
bool aIncludeExtraFiles )
115 wxT(
"*.kicad_pro" ),
116 wxT(
"*.kicad_prl" ),
117 wxT(
"*.kicad_sch" ),
118 wxT(
"*.kicad_sym" ),
119 wxT(
"*.kicad_pcb" ),
120 wxT(
"*.kicad_mod" ),
121 wxT(
"*.kicad_dru" ),
122 wxT(
"*.kicad_wks" ),
123 wxT(
"fp-lib-table" ),
124 wxT(
"sym-lib-table" )
128 static const wxChar* extraExtensionList[] = {
131 wxT(
"*.lib" ), wxT(
"*.dcm" ),
135 wxT(
"*.stp" ), wxT(
"*.step" ),
137 wxT(
"*.g?" ), wxT(
"*.g??" ),
138 wxT(
"*.gm??" ), wxT(
"*.gbrjob" ),
139 wxT(
"*.pos" ), wxT(
"*.drl" ), wxT(
"*.nc" ), wxT(
"*.xnc" ),
146 wxT(
"*.cir" ), wxT(
"*.sub" ), wxT(
"*.model" ),
152 wxString oldCwd = wxGetCwd();
154 wxSetWorkingDirectory( aSrcDir );
156 wxFFileOutputStream ostream( aDestFile );
158 if( !ostream.IsOk() )
160 msg.Printf(
_(
"Failed to create file '%s'." ), aDestFile );
165 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
168 wxString currFilename;
175 if( aIncludeExtraFiles )
177 for(
unsigned ii = 0; ii <
arrayDim( extraExtensionList ); ii++ )
178 wxDir::GetAllFiles( aSrcDir, &files, extraExtensionList[ii] );
181 for(
unsigned ii = 0; ii < files.GetCount(); ++ii )
183 if( files[ii].EndsWith( wxS(
".ibs" ) ) )
185 wxFileName package( files[ ii ] );
186 package.MakeRelativeTo( aSrcDir );
187 package.SetExt( wxS(
"pkg" ) );
189 if( package.Exists() )
190 files.push_back( package.GetFullName() );
196 unsigned long uncompressedBytes = 0;
198 for(
unsigned ii = 0; ii < files.GetCount(); ii++ )
202 wxFileName curr_fn( files[ii] );
203 curr_fn.MakeRelativeTo( aSrcDir );
204 currFilename = curr_fn.GetFullPath();
207 wxFSFile* infile = fsfile.OpenFile( wxFileSystem::FileNameToURL( curr_fn ) );
211 zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
212 infile->GetStream()->Read( zipstream );
213 zipstream.CloseEntry();
215 uncompressedBytes += infile->GetStream()->GetSize();
219 msg.Printf(
_(
"Archived file '%s'." ), currFilename );
229 msg.Printf(
_(
"Failed to archive file '%s'." ), currFilename );
238 [](
unsigned long aSize ) -> wxString
240 constexpr float KB = 1024.0;
241 constexpr float MB = KB * 1024.0;
245 else if( aSize >= KB )
251 size_t zipBytesCnt = ostream.GetSize();
253 if( zipstream.Close() )
255 msg.Printf(
_(
"Zip archive '%s' created (%s uncompressed, %s compressed)." ),
257 reportSize( uncompressedBytes ),
258 reportSize( zipBytesCnt ) );
263 msg.Printf( wxT(
"Failed to create file '%s'." ), aDestFile );
268 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.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
static const std::vector< std::string > extensionList
Definition of file extensions used in Kicad.
static bool CopyStreamData(wxInputStream &inputStream, wxOutputStream &outputStream, wxFileOffset size)