1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-28 09:24:14 +01:00

Add library from ZIP

This commit is contained in:
Federico Fissore 2012-10-17 15:25:29 +02:00
parent 0efc024f93
commit e8ea12c629
6 changed files with 212 additions and 2 deletions

View File

@ -36,6 +36,7 @@ import processing.app.helpers.FileUtils;
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.filefilters.OnlyDirs;
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
import processing.app.tools.ZipDeflater;
import processing.core.*;
import static processing.app.I18n._;
@ -2594,4 +2595,25 @@ public class Base {
}
}
}
public void handleAddZipLibrary(Editor editor) {
String prompt = _("Select a zip file containing the library you'd like to add");
FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
fd.setDirectory(System.getProperty("user.home"));
fd.setVisible(true);
String directory = fd.getDirectory();
String filename = fd.getFile();
if (filename == null) return;
File sourceFile = new File(directory, filename);
try {
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
zipDeflater.deflate();
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
} catch (IOException e) {
editor.statusError(e);
}
}
}

View File

@ -178,7 +178,7 @@ public class Editor extends JFrame implements RunnerListener {
// re-add the sub-menus that are shared by all windows
fileMenu.insert(sketchbookMenu, 2);
fileMenu.insert(examplesMenu, 3);
sketchMenu.insert(importMenu, 4);
//sketchMenu.insert(importMenu, 4);
toolsMenu.insert(boardsMenu, numTools);
toolsMenu.insert(cpuTypeMenu, numTools + 1);
toolsMenu.insert(serialMenu, numTools + 2);
@ -190,7 +190,7 @@ public class Editor extends JFrame implements RunnerListener {
// System.err.println("deactivate"); // not coming through
fileMenu.remove(sketchbookMenu);
fileMenu.remove(examplesMenu);
sketchMenu.remove(importMenu);
//sketchMenu.remove(importMenu);
toolsMenu.remove(boardsMenu);
toolsMenu.remove(cpuTypeMenu);
toolsMenu.remove(serialMenu);
@ -634,6 +634,16 @@ public class Editor extends JFrame implements RunnerListener {
}
sketchMenu.add(importMenu);
item = new JMenuItem(_("Add Library from ZIP"));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
base.handleAddZipLibrary(Editor.this);
base.onBoardOrPortChange();
base.rebuildImportMenu(Editor.importMenu);
}
});
sketchMenu.add(item);
item = newJMenuItem(_("Show Sketch Folder"), 'K');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

View File

@ -0,0 +1,97 @@
package processing.app.tools;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class ZipDeflater {
private final ZipFile zipFile;
private final File destFolder;
public ZipDeflater(File file, File destFolder) throws ZipException, IOException {
this.destFolder = destFolder;
this.zipFile = new ZipFile(file);
}
public void deflate() throws IOException {
String folderName = tempFolderNameFromZip();
File folder = new File(destFolder, folderName);
if (!folder.mkdir()) {
throw new IOException("Unable to create folder " + folderName);
}
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
ensureFoldersOfEntryExist(folder, entry);
File entryFile = new File(folder, entry.getName());
if (entry.isDirectory()) {
entryFile.mkdir();
} else {
FileOutputStream fos = null;
InputStream zipInputStream = null;
try {
fos = new FileOutputStream(entryFile);
zipInputStream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1024 * 4];
int len = -1;
while ((len = zipInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} finally {
if (fos != null) {
fos.close();
}
if (zipInputStream != null) {
zipInputStream.close();
}
}
}
}
// 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
ensureOneLevelFolder(folder);
}
private void ensureFoldersOfEntryExist(File folder, ZipEntry entry) {
String[] parts = entry.getName().split("/");
File current = folder;
for (int i = 0; i < parts.length - 1; i++) {
current = new File(current, parts[i]);
current.mkdir();
}
}
private void ensureOneLevelFolder(File folder) {
File[] files = folder.listFiles();
if (files.length == 1 && files[0].isDirectory()) {
File tempFile = new File(files[0].getPath() + new Random().nextInt(1000));
files[0].renameTo(tempFile);
for (File file : tempFile.listFiles()) {
file.renameTo(new File(folder, file.getName()));
}
tempFile.delete();
}
}
private String tempFolderNameFromZip() {
String folderName = zipFile.getName();
if (folderName.lastIndexOf(".") != -1) {
folderName = folderName.substring(0, folderName.lastIndexOf("."));
}
if (folderName.lastIndexOf(File.separator) != -1) {
folderName = folderName.substring(folderName.lastIndexOf(File.separator) + 1);
}
return folderName;
}
}

BIN
app/test/Test.zip Normal file

Binary file not shown.

BIN
app/test/Test2.zip Normal file

Binary file not shown.

View File

@ -0,0 +1,81 @@
package processing.app.tools;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.Arrays;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import processing.app.tools.ZipDeflater;
public class ZipDeflaterTest {
private File destFolder;
@Before
public void makeTempFolder() {
destFolder = new File(System.getProperty("java.io.tmpdir") + File.separator + "arduino_zip_test_" + new Random().nextInt(100000));
destFolder.mkdir();
}
@Test
public void shouldDeflateZip() throws Exception {
File file = new File(ZipDeflater.class.getResource("/Test2.zip").getFile());
new ZipDeflater(file, destFolder).deflate();
String[] files = destFolder.list();
assertEquals(1, files.length);
assertEquals("Test2", files[0]);
file = destFolder.listFiles()[0];
files = file.list();
assertEquals(5, files.length);
Arrays.sort(files);
assertEquals("Test.cpp", files[0]);
assertEquals("Test.h", files[1]);
assertEquals("examples", files[2]);
assertEquals("keywords.txt", files[3]);
assertEquals("readme.txt", files[4]);
}
@Test
public void shouldDeflateZipAndMoveContentsToParentFolder() throws Exception {
File file = new File(ZipDeflater.class.getResource("/Test.zip").getFile());
new ZipDeflater(file, destFolder).deflate();
String[] files = destFolder.list();
assertEquals(1, files.length);
assertEquals("Test", files[0]);
file = destFolder.listFiles()[0];
files = file.list();
assertEquals(5, files.length);
Arrays.sort(files);
assertEquals("Test.cpp", files[0]);
assertEquals("Test.h", files[1]);
assertEquals("examples", files[2]);
assertEquals("keywords.txt", files[3]);
assertEquals("readme.txt", files[4]);
}
@After
public void deleteTempFolder() {
recursiveDelete(destFolder);
}
private void recursiveDelete(File folder) {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
recursiveDelete(file);
} else {
file.delete();
}
}
folder.delete();
}
}