Skip to content

Commit eda3bd8

Browse files
committed
Add library from ZIP
Conflicts: app/src/processing/app/Base.java
1 parent d003c53 commit eda3bd8

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

app/src/processing/app/Base.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import processing.app.debug.Compiler;
3333
import processing.app.debug.Target;
34+
import processing.app.tools.ZipDeflater;
3435
import processing.core.*;
3536
import static processing.app.I18n._;
3637

@@ -2362,4 +2363,25 @@ static protected void listFiles(String basePath,
23622363
}
23632364
}
23642365
}
2366+
2367+
2368+
public void handleAddZipLibrary(Editor editor) {
2369+
String prompt = _("Select a zip file containing the library you'd like to add");
2370+
FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
2371+
fd.setDirectory(System.getProperty("user.home"));
2372+
fd.setVisible(true);
2373+
2374+
String directory = fd.getDirectory();
2375+
String filename = fd.getFile();
2376+
if (filename == null) return;
2377+
2378+
File sourceFile = new File(directory, filename);
2379+
try {
2380+
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
2381+
zipDeflater.deflate();
2382+
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
2383+
} catch (IOException e) {
2384+
editor.statusError(e);
2385+
}
2386+
}
23652387
}

app/src/processing/app/Editor.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void windowActivated(WindowEvent e) {
177177
// re-add the sub-menus that are shared by all windows
178178
fileMenu.insert(sketchbookMenu, 2);
179179
fileMenu.insert(examplesMenu, 3);
180-
sketchMenu.insert(importMenu, 4);
180+
//sketchMenu.insert(importMenu, 4);
181181
toolsMenu.insert(boardsMenu, numTools);
182182
toolsMenu.insert(serialMenu, numTools + 1);
183183
}
@@ -188,7 +188,7 @@ public void windowDeactivated(WindowEvent e) {
188188
// System.err.println("deactivate"); // not coming through
189189
fileMenu.remove(sketchbookMenu);
190190
fileMenu.remove(examplesMenu);
191-
sketchMenu.remove(importMenu);
191+
//sketchMenu.remove(importMenu);
192192
toolsMenu.remove(boardsMenu);
193193
toolsMenu.remove(serialMenu);
194194
}
@@ -631,6 +631,16 @@ public void actionPerformed(ActionEvent e) {
631631
}
632632
sketchMenu.add(importMenu);
633633

634+
item = new JMenuItem(_("Add Library from ZIP"));
635+
item.addActionListener(new ActionListener() {
636+
public void actionPerformed(ActionEvent e) {
637+
base.handleAddZipLibrary(Editor.this);
638+
base.onBoardOrPortChange();
639+
base.rebuildImportMenu(Editor.importMenu);
640+
}
641+
});
642+
sketchMenu.add(item);
643+
634644
item = newJMenuItem(_("Show Sketch Folder"), 'K');
635645
item.addActionListener(new ActionListener() {
636646
public void actionPerformed(ActionEvent e) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package processing.app.tools;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.Enumeration;
8+
import java.util.Random;
9+
import java.util.zip.ZipEntry;
10+
import java.util.zip.ZipException;
11+
import java.util.zip.ZipFile;
12+
13+
public class ZipDeflater {
14+
15+
private final ZipFile zipFile;
16+
private final File destFolder;
17+
18+
public ZipDeflater(File file, File destFolder) throws ZipException, IOException {
19+
this.destFolder = destFolder;
20+
this.zipFile = new ZipFile(file);
21+
}
22+
23+
public void deflate() throws IOException {
24+
String folderName = tempFolderNameFromZip();
25+
26+
File folder = new File(destFolder, folderName);
27+
28+
if (!folder.mkdir()) {
29+
throw new IOException("Unable to create folder " + folderName);
30+
}
31+
32+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
33+
while (entries.hasMoreElements()) {
34+
ZipEntry entry = entries.nextElement();
35+
ensureFoldersOfEntryExist(folder, entry);
36+
File entryFile = new File(folder, entry.getName());
37+
if (entry.isDirectory()) {
38+
entryFile.mkdir();
39+
} else {
40+
FileOutputStream fos = null;
41+
InputStream zipInputStream = null;
42+
try {
43+
fos = new FileOutputStream(entryFile);
44+
zipInputStream = zipFile.getInputStream(entry);
45+
byte[] buffer = new byte[1024 * 4];
46+
int len = -1;
47+
while ((len = zipInputStream.read(buffer)) != -1) {
48+
fos.write(buffer, 0, len);
49+
}
50+
} finally {
51+
if (fos != null) {
52+
fos.close();
53+
}
54+
if (zipInputStream != null) {
55+
zipInputStream.close();
56+
}
57+
}
58+
}
59+
}
60+
61+
// Test.zip may or may not contain Test folder. We use zip name to create libraries folder. Therefore, a contained Test folder is useless and must be removed
62+
ensureOneLevelFolder(folder);
63+
}
64+
65+
private void ensureFoldersOfEntryExist(File folder, ZipEntry entry) {
66+
String[] parts = entry.getName().split("/");
67+
File current = folder;
68+
for (int i = 0; i < parts.length - 1; i++) {
69+
current = new File(current, parts[i]);
70+
current.mkdir();
71+
}
72+
}
73+
74+
private void ensureOneLevelFolder(File folder) {
75+
File[] files = folder.listFiles();
76+
if (files.length == 1 && files[0].isDirectory()) {
77+
File tempFile = new File(files[0].getPath() + new Random().nextInt(1000));
78+
files[0].renameTo(tempFile);
79+
for (File file : tempFile.listFiles()) {
80+
file.renameTo(new File(folder, file.getName()));
81+
}
82+
tempFile.delete();
83+
}
84+
}
85+
86+
private String tempFolderNameFromZip() {
87+
String folderName = zipFile.getName();
88+
if (folderName.lastIndexOf(".") != -1) {
89+
folderName = folderName.substring(0, folderName.lastIndexOf("."));
90+
}
91+
if (folderName.lastIndexOf(File.separator) != -1) {
92+
folderName = folderName.substring(folderName.lastIndexOf(File.separator) + 1);
93+
}
94+
return folderName;
95+
}
96+
97+
}

0 commit comments

Comments
 (0)