mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 16:47:31 -05:00
#829 Integrates JMBE Creator with new user preferences editor for auto-downloading and compiling the JMBE library from within sdrtrunk. (#844)
Co-authored-by: Denny <denny@denny-desktop>
This commit is contained in:
76
src/main/java/io/github/dsheirer/util/FileUtil.java
Normal file
76
src/main/java/io/github/dsheirer/util/FileUtil.java
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2020 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* File Utilities
|
||||
*/
|
||||
public class FileUtil
|
||||
{
|
||||
/**
|
||||
* Recursively searches for the named file starting in the specified directory.
|
||||
* @param directory to search for the file
|
||||
* @return path to the file or null if the file can't be found
|
||||
* @throws IOException if there is a file system error or if the file is not found
|
||||
*/
|
||||
public static Path findFile(Path directory, String name) throws IOException
|
||||
{
|
||||
if(Files.isDirectory(directory))
|
||||
{
|
||||
DirectoryStream<Path> stream = Files.newDirectoryStream(directory);
|
||||
Iterator<Path> it = stream.iterator();
|
||||
Path path = null;
|
||||
while(it.hasNext())
|
||||
{
|
||||
path = it.next();
|
||||
|
||||
if(Files.isDirectory(path))
|
||||
{
|
||||
Path candidate = findFile(path, name);
|
||||
|
||||
if(candidate != null)
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(path.endsWith(name))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IOException("A directory required for this method [" +
|
||||
(directory != null ? directory.toString() : "null") + "]");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
101
src/main/java/io/github/dsheirer/util/OSType.java
Normal file
101
src/main/java/io/github/dsheirer/util/OSType.java
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2020 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Locale;
|
||||
|
||||
public enum OSType
|
||||
{
|
||||
LINUX_32,
|
||||
LINUX_64,
|
||||
OSX_32,
|
||||
OSX_64,
|
||||
WINDOWS_32,
|
||||
WINDOWS_64,
|
||||
UNKNOWN;
|
||||
|
||||
public static EnumSet<OSType> WINDOWS_TYPES = EnumSet.of(WINDOWS_32, WINDOWS_64);
|
||||
public static EnumSet<OSType> LINUX_TYPES = EnumSet.of(LINUX_32, LINUX_64, OSX_32, OSX_64);
|
||||
|
||||
/**
|
||||
* Indicates if this enumeration entry is a Windows type
|
||||
*/
|
||||
public boolean isWindows()
|
||||
{
|
||||
return WINDOWS_TYPES.contains(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this enumeration entry is a Linux type
|
||||
*/
|
||||
public boolean isLinux()
|
||||
{
|
||||
return LINUX_TYPES.contains(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects the current host operating system and architecture
|
||||
*/
|
||||
public static OSType getCurrentOSType()
|
||||
{
|
||||
String os = System.getProperty("os.name", "unknown").toLowerCase(Locale.ENGLISH);
|
||||
String arch = System.getProperty("os.arch");
|
||||
|
||||
if(os.contains("win"))
|
||||
{
|
||||
if(arch.contains("64"))
|
||||
{
|
||||
return OSType.WINDOWS_64;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OSType.WINDOWS_32;
|
||||
}
|
||||
}
|
||||
|
||||
if(os.contains("mac") || os.contains("darwin"))
|
||||
{
|
||||
if(arch.contains("64"))
|
||||
{
|
||||
return OSType.OSX_64;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OSType.OSX_32;
|
||||
}
|
||||
}
|
||||
|
||||
if(os.contains("nux") || os.contains("nix") || os.contains("aix"))
|
||||
{
|
||||
if(arch.contains("64"))
|
||||
{
|
||||
return OSType.LINUX_64;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OSType.LINUX_32;
|
||||
}
|
||||
}
|
||||
|
||||
return OSType.UNKNOWN;
|
||||
}
|
||||
|
||||
}
|
||||
158
src/main/java/io/github/dsheirer/util/ZipUtility.java
Normal file
158
src/main/java/io/github/dsheirer/util/ZipUtility.java
Normal file
@@ -0,0 +1,158 @@
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import org.rauschig.jarchivelib.ArchiveFormat;
|
||||
import org.rauschig.jarchivelib.Archiver;
|
||||
import org.rauschig.jarchivelib.ArchiverFactory;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* This utility compresses a list of files to standard ZIP format file.
|
||||
* It is able to compress all sub files and sub directories, recursively.
|
||||
*
|
||||
* @author www.codejava.net
|
||||
*/
|
||||
public class ZipUtility
|
||||
{
|
||||
/**
|
||||
* A constants for buffer size used to read/write data
|
||||
*/
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
|
||||
/**
|
||||
* Compresses a list of files to a destination zip file
|
||||
*
|
||||
* @param listFiles A collection of files and directories
|
||||
* @param destZipFile The path of the destination zip file
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
public void zip(List<File> listFiles, String destZipFile) throws FileNotFoundException,
|
||||
IOException
|
||||
{
|
||||
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile));
|
||||
for(File file : listFiles)
|
||||
{
|
||||
if(file.isDirectory())
|
||||
{
|
||||
zipDirectory(file, file.getName(), zos);
|
||||
}
|
||||
else
|
||||
{
|
||||
zipFile(file, zos);
|
||||
}
|
||||
}
|
||||
zos.flush();
|
||||
zos.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses files represented in an array of paths
|
||||
*
|
||||
* @param files a String array containing file paths
|
||||
* @param destZipFile The path of the destination zip file
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
public void zip(String[] files, String destZipFile) throws FileNotFoundException, IOException
|
||||
{
|
||||
List<File> listFiles = new ArrayList<File>();
|
||||
|
||||
for(int i = 0; i < files.length; i++)
|
||||
{
|
||||
listFiles.add(new File(files[i]));
|
||||
}
|
||||
|
||||
zip(listFiles, destZipFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a directory to the current zip output stream
|
||||
*
|
||||
* @param folder the directory to be added
|
||||
* @param parentFolder the path of parent directory
|
||||
* @param zos the current zip output stream
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
private void zipDirectory(File folder, String parentFolder, ZipOutputStream zos) throws
|
||||
FileNotFoundException, IOException
|
||||
{
|
||||
if(folder == null || folder.listFiles() == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for(File file : folder.listFiles())
|
||||
{
|
||||
if(file.exists())
|
||||
{
|
||||
if(file.isDirectory())
|
||||
{
|
||||
zipDirectory(file, parentFolder + "/" + file.getName(), zos);
|
||||
continue;
|
||||
}
|
||||
|
||||
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
long bytesRead = 0;
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
while((read = bis.read(bytesIn)) != -1)
|
||||
{
|
||||
zos.write(bytesIn, 0, read);
|
||||
bytesRead += read;
|
||||
}
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a file to the current zip output stream
|
||||
*
|
||||
* @param file the file to be added
|
||||
* @param zos the current zip output stream
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
private void zipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException
|
||||
{
|
||||
zos.putNextEntry(new ZipEntry(file.getName()));
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
long bytesRead = 0;
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
while((read = bis.read(bytesIn)) != -1)
|
||||
{
|
||||
zos.write(bytesIn, 0, read);
|
||||
bytesRead += read;
|
||||
}
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unzips the specified file into the directory where it's located
|
||||
*
|
||||
* @param zipFile to unzip
|
||||
* @return path to where the file was unzipped.
|
||||
* @throws IOException if there are errors unzipping
|
||||
*/
|
||||
public static Path unzip(Path zipFile) throws IOException
|
||||
{
|
||||
Path parent = zipFile.getParent();
|
||||
Archiver archiver = ArchiverFactory.createArchiver(ArchiveFormat.ZIP);
|
||||
archiver.extract(zipFile.toFile(), parent.toFile());
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user