22
33import java .io .File ;
44import java .io .IOException ;
5+ import java .io .FileInputStream ;
56import java .nio .file .Files ;
67import java .util .*;
78import java .util .stream .Collectors ;
89import java .util .stream .Stream ;
10+ import java .util .zip .ZipEntry ;
11+ import java .util .zip .ZipOutputStream ;
12+ import java .io .FileOutputStream ;
913
1014import cc .arduino .files .DeleteFilesOnShutdown ;
1115import processing .app .helpers .FileUtils ;
16+ import processing .app .tools .ZipDeflater ;
17+
18+ import org .apache .commons .compress .utils .IOUtils ;
1219
1320import static processing .app .I18n .tr ;
1421
1724 */
1825public class Sketch {
1926 public static final String DEFAULT_SKETCH_EXTENSION = "ino" ;
27+ public static final String DEFAULT_SKETCH_EXTENSION_COMPRESSED = "inoz" ;
2028 public static final List <String > OLD_SKETCH_EXTENSIONS = Arrays .asList ("pde" );
21- public static final List <String > SKETCH_EXTENSIONS = Stream .concat (Stream .of (DEFAULT_SKETCH_EXTENSION ), OLD_SKETCH_EXTENSIONS .stream ()).collect (Collectors .toList ());
29+ public static final List <String > DEFAULT_SKETCH_EXTENSIONS = Stream .concat (Stream .of (DEFAULT_SKETCH_EXTENSION ), Stream .of (DEFAULT_SKETCH_EXTENSION_COMPRESSED )).collect (Collectors .toList ());
30+ public static final List <String > SKETCH_EXTENSIONS = Stream .concat (DEFAULT_SKETCH_EXTENSIONS .stream (), OLD_SKETCH_EXTENSIONS .stream ()).collect (Collectors .toList ());
2231 public static final List <String > OTHER_ALLOWED_EXTENSIONS = Arrays .asList ("c" , "cpp" , "h" , "hh" , "hpp" , "s" );
2332 public static final List <String > EXTENSIONS = Stream .concat (SKETCH_EXTENSIONS .stream (), OTHER_ALLOWED_EXTENSIONS .stream ()).collect (Collectors .toList ());
2433
@@ -27,6 +36,8 @@ public class Sketch {
2736 */
2837 private File folder ;
2938
39+ private File compressedSketch = null ;
40+
3041 private List <SketchFile > files = new ArrayList <>();
3142
3243 private File buildPath ;
@@ -50,10 +61,70 @@ public int compare(SketchFile x, SketchFile y) {
5061 * Any file inside the sketch directory.
5162 */
5263 Sketch (File file ) throws IOException {
53- folder = file .getParentFile ();
64+ if (file .getName ().endsWith ("inz" )) {
65+ //extract it in a temp folder and assign the
66+ compressedSketch = file ;
67+ File tmpFolder = FileUtils .createTempFolder ();
68+ ZipDeflater zipDeflater = new ZipDeflater (file , tmpFolder );
69+ zipDeflater .deflate ();
70+ String basename = FileUtils .splitFilename (file .getName ()).basename ;
71+ folder = new File (tmpFolder , basename );
72+ } else {
73+ folder = file .getParentFile ();
74+ }
5475 files = listSketchFiles (true );
5576 }
5677
78+ public void buildZip (File dir , String sofar ,
79+ ZipOutputStream zos ) throws IOException {
80+ String files [] = dir .list ();
81+ if (files == null ) {
82+ throw new IOException ("Unable to list files from " + dir );
83+ }
84+ for (int i = 0 ; i < files .length ; i ++) {
85+ if (files [i ].equals ("." ) ||
86+ files [i ].equals (".." )) continue ;
87+
88+ File sub = new File (dir , files [i ]);
89+ String nowfar = (sofar == null ) ?
90+ files [i ] : (sofar + "/" + files [i ]);
91+
92+ if (sub .isDirectory ()) {
93+ // directories are empty entries and have / at the end
94+ ZipEntry entry = new ZipEntry (nowfar + "/" );
95+ //System.out.println(entry);
96+ zos .putNextEntry (entry );
97+ zos .closeEntry ();
98+ buildZip (sub , nowfar , zos );
99+
100+ } else {
101+ ZipEntry entry = new ZipEntry (nowfar );
102+ entry .setTime (sub .lastModified ());
103+ zos .putNextEntry (entry );
104+ zos .write (loadBytesRaw (sub ));
105+ zos .closeEntry ();
106+ }
107+ }
108+ }
109+
110+ static public byte [] loadBytesRaw (File file ) throws IOException {
111+ int size = (int ) file .length ();
112+ FileInputStream input = null ;
113+ try {
114+ input = new FileInputStream (file );
115+ byte buffer [] = new byte [size ];
116+ int offset = 0 ;
117+ int bytesRead ;
118+ while ((bytesRead = input .read (buffer , offset , size - offset )) != -1 ) {
119+ offset += bytesRead ;
120+ if (bytesRead == 0 ) break ;
121+ }
122+ return buffer ;
123+ } finally {
124+ IOUtils .closeQuietly (input );
125+ }
126+ }
127+
57128 static public File checkSketchFile (File file ) {
58129 // check to make sure that this .pde file is
59130 // in a folder of the same name
@@ -137,6 +208,13 @@ public void save() throws IOException {
137208 if (file .isModified ())
138209 file .save ();
139210 }
211+ String basename = files .get (0 ).getPrettyName ();
212+ if (compressedSketch != null ) {
213+ ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (compressedSketch ));
214+ buildZip (folder , basename , zos );
215+ zos .flush ();
216+ IOUtils .closeQuietly (zos );
217+ }
140218 }
141219
142220 public int getCodeCount () {
@@ -151,6 +229,9 @@ public SketchFile[] getFiles() {
151229 * Returns a file object for the primary .pde of this sketch.
152230 */
153231 public SketchFile getPrimaryFile () {
232+ if (compressedSketch != null ) {
233+ return new SketchFile (this , compressedSketch );
234+ }
154235 return files .get (0 );
155236 }
156237
0 commit comments