mirror of
https://github.com/magefree/mage.git
synced 2026-01-10 12:52:06 -08:00
[Updater] more refactoring
This commit is contained in:
parent
d819774cc6
commit
7b1ffb6ced
3 changed files with 55 additions and 51 deletions
|
|
@ -0,0 +1,49 @@
|
|||
package com.magefree.update.helpers;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* @author Loki
|
||||
*/
|
||||
public class ChechsumHelper {
|
||||
|
||||
public static byte[] createChecksum(String filename) throws Exception {
|
||||
InputStream fis = null;
|
||||
MessageDigest complete;
|
||||
|
||||
try {
|
||||
fis = new FileInputStream(filename);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
complete = MessageDigest.getInstance("SHA1");
|
||||
|
||||
int numRead;
|
||||
do {
|
||||
numRead = fis.read(buffer);
|
||||
if (numRead > 0) {
|
||||
complete.update(buffer, 0, numRead);
|
||||
}
|
||||
} while (numRead != -1);
|
||||
|
||||
return complete.digest();
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// see this How-to for a faster way to convert
|
||||
// a byte array to a HEX string
|
||||
public static String getSHA1Checksum(String filename) throws Exception {
|
||||
byte[] b = createChecksum(filename);
|
||||
String result = "";
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
result +=
|
||||
Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
package com.magefree.update.helpers;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helper for file operations.
|
||||
*
|
||||
* @author noxx
|
||||
*/
|
||||
public class FileHelper {
|
||||
|
||||
private FileHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters out dirs.
|
||||
*/
|
||||
private static final FilenameFilter anyFileFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return dir.isFile();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Filters out jars.
|
||||
*/
|
||||
private static final FilenameFilter jarFileFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(".jar");
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets .jar files from specified folder.
|
||||
*
|
||||
* @param dir Folder to scan for rile
|
||||
* @return
|
||||
*/
|
||||
public static List<File> findJarsInDir(String dir) {
|
||||
ArrayList<File> result = new ArrayList<File>();
|
||||
File directory = new File(dir);
|
||||
if (directory.exists() && directory.isDirectory()) {
|
||||
for (File jar : directory.listFiles(jarFileFilter)) {
|
||||
result.add(jar);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets non-dir files from specified folder.
|
||||
*
|
||||
* @param dir Folder to scan for rile
|
||||
* @return
|
||||
*/
|
||||
public static List<File> findAllFilesInDir(String dir) {
|
||||
ArrayList<File> result = new ArrayList<File>();
|
||||
File directory = new File(dir);
|
||||
if (directory.exists() && directory.isDirectory()) {
|
||||
for (File jar : directory.listFiles(anyFileFilter)) {
|
||||
result.add(jar);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all files from the list.
|
||||
*
|
||||
* @param files
|
||||
*/
|
||||
public static void removeFiles(List<String> files) {
|
||||
for (String filename : files) {
|
||||
File f = new File(filename);
|
||||
if (f.exists()) {
|
||||
f.delete();
|
||||
System.out.println("File has been deleted: " + filename);
|
||||
} else {
|
||||
System.out.println("ERROR. Couldn't find file to delete: " + filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads specified file.
|
||||
*
|
||||
* @param filename
|
||||
* @param urlConnection
|
||||
*/
|
||||
public static void downloadFile(String filename, HttpURLConnection urlConnection) {
|
||||
System.out.println("Downloading " + filename);
|
||||
try {
|
||||
InputStream in = urlConnection.getInputStream();
|
||||
File f = new File(filename);
|
||||
if (!f.exists()) {
|
||||
f.getParentFile().mkdirs();
|
||||
System.out.println("Directories have been created: " + f.getParentFile().getPath());
|
||||
}
|
||||
|
||||
FileOutputStream out = new FileOutputStream(filename);
|
||||
byte[] buf = new byte[4 * 1024];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = in.read(buf)) != -1) {
|
||||
out.write(buf, 0, bytesRead);
|
||||
}
|
||||
|
||||
System.out.println("File has been updated: " + filename);
|
||||
} catch (IOException e) {
|
||||
System.out.println("i/o exception - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue