21 #include <wx/filedlg.h> 22 #include <wx/fs_zip.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(
"*.gb?" ), wxT(
"*.gbrjob" ),
138 wxT(
"*.gko" ), wxT(
"*.gm1" ),
139 wxT(
"*.gm2" ), wxT(
"*.g?" ),
140 wxT(
"*.gp1" ), wxT(
"*.gp2" ),
141 wxT(
"*.gpb" ), wxT(
"*.gpt" ),
143 wxT(
"*.pos" ), wxT(
"*.drl" ), wxT(
"*.nc" ), wxT(
"*.xnc" ),
144 wxT(
"*.d356" ), wxT(
"*.rpt" ),
145 wxT(
"*.net" ), wxT(
"*.py" ),
146 wxT(
"*.pdf" ), wxT(
"*.txt" )
151 wxString oldCwd = wxGetCwd();
153 wxSetWorkingDirectory( aSrcDir );
155 wxFFileOutputStream ostream( aDestFile );
157 if( !ostream.IsOk() )
159 msg.Printf(
_(
"Failed to create file '%s'." ), aDestFile );
164 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
167 wxString currFilename;
174 if( aIncludeExtraFiles )
176 for(
unsigned ii = 0; ii <
arrayDim( extraExtensionList ); ii++ )
177 wxDir::GetAllFiles( aSrcDir, &files, extraExtensionList[ii] );
182 unsigned long uncompressedBytes = 0;
184 for(
unsigned ii = 0; ii < files.GetCount(); ii++ )
188 wxFileName curr_fn( files[ii] );
189 curr_fn.MakeRelativeTo( aSrcDir );
190 currFilename = curr_fn.GetFullPath();
193 wxFSFile* infile = fsfile.OpenFile( wxFileSystem::FileNameToURL( curr_fn ) );
197 zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
198 infile->GetStream()->Read( zipstream );
199 zipstream.CloseEntry();
201 uncompressedBytes += infile->GetStream()->GetSize();
205 msg.Printf(
_(
"Archived file '%s'." ), currFilename );
215 msg.Printf(
_(
"Failed to archive file '%s'." ), currFilename );
224 [](
unsigned long aSize ) -> wxString
226 constexpr
float KB = 1024.0;
227 constexpr
float MB = KB * 1024.0;
231 else if( aSize >= KB )
237 size_t zipBytesCnt = ostream.GetSize();
239 if( zipstream.Close() )
241 msg.Printf(
_(
"Zip archive '%s' created (%s uncompressed, %s compressed)." ),
243 reportSize( uncompressedBytes ),
244 reportSize( zipBytesCnt ) );
249 msg.Printf( wxT(
"Failed to create file '%s'." ), aDestFile );
254 wxSetWorkingDirectory( oldCwd );
bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Creates an archive of the project.
static const std::vector< std::string > extensionList
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 bool CopyStreamData(wxInputStream &inputStream, wxOutputStream &outputStream, wxFileOffset size)
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 ...
Definition of file extensions used in Kicad.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.