diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index bb5d4d55a..30ba20136 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -2382,7 +2382,7 @@ public class Base { Dimension preferredSize = fileChooser.getPreferredSize(); fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200)); - + int returnVal = fileChooser.showOpenDialog(editor); if (returnVal != JFileChooser.APPROVE_OPTION) { @@ -2390,8 +2390,39 @@ public class Base { } File sourceFile = fileChooser.getSelectedFile(); + File tmpFolder = null; - if (sourceFile.isDirectory()) { + try { + // unpack ZIP + if (!sourceFile.isDirectory()) { + try { + tmpFolder = FileUtils.createTempFolder(); + ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder); + zipDeflater.deflate(); + File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs()); + if (foldersInTmpFolder.length != 1) { + throw new IOException("Zip doesn't contain one library"); + } + sourceFile = foldersInTmpFolder[0]; + } catch (IOException e) { + editor.statusError(e); + return; + } + } + + // is there a library? + File libFolder = scanFatLibrary(sourceFile); + if (libFolder == null) { + editor.statusError("Not a valid library"); + return; + } + String[] headerFiles = headerListFromIncludePath(libFolder); + if (headerFiles == null || headerFiles.length == 0) { + editor.statusError("Not a valid library"); + return; + } + + // copy folder File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName()); if (!destinationFolder.mkdir()) { editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder"); @@ -2403,15 +2434,10 @@ public class Base { editor.statusError(e); return; } - } else { - try { - ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder()); - zipDeflater.deflate(); - } catch (IOException e) { - editor.statusError(e); - return; - } + editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu")); + } finally { + // delete zip created temp folder, if exists + FileUtils.recursiveDelete(tmpFolder); } - editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu")); } } diff --git a/app/src/processing/app/helpers/FileUtils.java b/app/src/processing/app/helpers/FileUtils.java index 5902db40b..47c5b0a32 100644 --- a/app/src/processing/app/helpers/FileUtils.java +++ b/app/src/processing/app/helpers/FileUtils.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Random; public class FileUtils { @@ -66,6 +67,9 @@ public class FileUtils { } public static void recursiveDelete(File file) { + if (file == null) { + return; + } if (file.isDirectory()) { for (File current : file.listFiles()) { if (current.isDirectory()) { @@ -78,4 +82,12 @@ public class FileUtils { file.delete(); } + public static File createTempFolder() throws IOException { + File tmpFolder = new File(System.getProperty("java.io.tmpdir"), "arduino_" + new Random().nextInt(1000000)); + if (!tmpFolder.mkdir()) { + throw new IOException("Unable to create temp folder " + tmpFolder); + } + return tmpFolder; + } + }