Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Criptoconsola;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.URL;
- import java.nio.file.*;
- import java.util.*;
- import java.security.*;
- import java.security.spec.X509EncodedKeySpec;
- import javax.crypto.*;
- import javax.crypto.spec.*;
- public class Criptoconsola {
- static GestorDescarregas gd = new GestorDescarregas();
- static final int tamanyBlocCopia = 512;
- static final int tamanyBlocHash = 512;
- static final int tamanyBlocEncriptacio = 128;
- static final int tamanyBlocDescarrega = 4;
- static final int tamanyBlocEncriptacioAsimetrica = 16;
- static Process process;
- static final String extensioXifrat = ".xifrat";
- static final String extensioXifratAsimetric = ".xifratAsimetric";
- static final int longitutVectorInicialitzacio = 1024;
- static final int tamanyBlocEnviament = 64;
- static final int portEnviament = 5;
- public static void main(String[] args) throws Exception {
- PrintStream o = System.out;
- InputStreamReader r = new InputStreamReader(System.in);
- java.io.BufferedReader i = new BufferedReader(r);
- o.println("+---------------------------------------------+");
- o.println("| Criptoconsola 1.0 |");
- o.println("+---------------------------------------------+");
- o.println("");
- Path directoriTreball = obtenirDirectoriArrel();
- String separadorDirectoris = obtenirSeparadorDirectoris();
- if (directoriTreball == null) {
- System.out.println("ERROR: No es pot obtenir el directori arrel");
- System.exit(-1);
- }
- boolean fi = false;
- while (!fi) {
- // Mostrar prompt
- String ds = directoriTreball.toString();
- o.print(ds + ">");
- // Obtenir linia de comanda
- String liniaComanda = i.readLine();
- // Parsejar comanda
- String parts[] = liniaComanda.split(" ");
- // Processar comanda
- String comanda = parts[0].toUpperCase();
- if (comanda.equals("AJUDA")) {
- o.println("");
- o.println("AJUDA");
- o.println("");
- o.println(" Mostra aquesta ajuda");
- o.println("");
- o.println("COPIAR <nomfitxer1> <nomfitxer2>");
- o.println("");
- o.println(" Fa una copia del fitxer1 i la desa com fitxer2, sobreescrivint-lo si existeix");
- o.println("");
- o.println("ELIMINAR <nomfitxer>");
- o.println("");
- o.println(" Elimina el fitxer indicat. Demana confirmacio");
- o.println("");
- o.println("CD <nomdirectori>");
- o.println("");
- o.println(" Canvia el directori de treball al directori indicat");
- o.println("");
- o.println("DIR");
- o.println("");
- o.println(" Mostra els continguts del directori de treball");
- o.println("");
- o.println("HASH <nomfitxer>");
- o.println("");
- o.println(" Calcula i mostra el hash MD5 del fitxer");
- o.println("");
- o.println("ENCRIPTA <nomfitxer1> <nomfitxer2>");
- o.println("");
- o.println(" Encripta el fitxer1 i el deixa com fitxer2, sobreescrivint-lo si existeix. Demana una contrasenya");
- o.println("");
- o.println("SORTIR");
- o.println("");
- o.println(" Finalitza la sessio");
- o.println("");
- } else if (comanda.equals("DIR")) {
- Object[] paths = Files.list(directoriTreball).toArray();
- //
- for (Object c : paths) {
- //Path p = (Path)paths[i];
- System.out.println(c);
- }
- } else if (comanda.equals("CD")) {
- String subdirectori = parts[1];
- File f = new File(directoriTreball + separadorDirectoris + subdirectori);
- if (subdirectori.equals("..")) {
- if (f.isDirectory() && directoriTreball.getParent() == null) {
- System.out.println("Error: La ruta introducida no existe o es un fichero.");
- } else {
- directoriTreball = directoriTreball.getParent();
- }
- } else {
- if (!f.exists()) {
- System.out.println("La ruta introducida no existe");
- } else if (f.isDirectory()) {
- directoriTreball = directoriTreball.resolve(subdirectori);
- } else {
- System.out.println("Error: La ruta introducida es un fichero");
- }
- }
- } else if (comanda.equals("COPIAR")) {
- Scanner sc = new Scanner(System.in);
- boolean error = false;
- String nomFitxer1 = parts[1];
- String nomFitxer2 = parts[2];
- String rutaFitxer1 = directoriTreball.toString() + separadorDirectoris + nomFitxer1;
- String rutaFitxer2 = directoriTreball.toString() + separadorDirectoris + nomFitxer2;
- File f1 = new File(rutaFitxer1);
- File f2 = new File(rutaFitxer2);
- if (!f1.exists()) {
- o.println("El fichero 1 no existe");
- error = true;
- } else if (!f1.canRead()) {
- o.println("El fichero 1 no tiene privilegio de lectura");
- error = true;
- } else if (!f2.createNewFile()) {
- o.println("El fichero 2 no se puede crear");
- error = true;
- } else if (f2.exists() && !f2.canWrite()) {
- o.println("El fichero 2 existe y no tiene privilegio de escritura");
- error = true;
- }
- if (f2.exists() && f2.canWrite()) {
- System.out.println("Desea sobreescribir el fichero?");
- String respuesta = sc.nextLine();
- if (respuesta.toLowerCase() == "no") {
- error = false;
- o.println("No se sobrescribe.");
- } else {
- o.println("Sobreescribir archivo.");
- }
- }
- if (error == false) {
- try {
- FileInputStream streamFitxer1 = new FileInputStream(rutaFitxer1);
- FileOutputStream streamFitxer2 = new FileOutputStream(rutaFitxer2);
- byte[] bloc = new byte[tamanyBlocCopia];
- boolean fiCopia = false;
- int llegits;
- while (!fiCopia) {
- while ((llegits = streamFitxer1.read(bloc)) > 0) {
- streamFitxer2.write(bloc, 0, llegits);
- }
- fiCopia = true;
- }
- streamFitxer1.close();
- streamFitxer2.close();
- } catch (Exception e) {
- System.out.println("Error en la ejecucion");
- }
- }
- } else if (comanda.equals("ELIMINAR")) {
- boolean encontrado = false;
- String nomFitxer = parts[1];
- String rutaFitxer = directoriTreball.toString() + separadorDirectoris + nomFitxer;
- Scanner sc = new Scanner(System.in);
- File fitxer = new File(rutaFitxer);
- if (!fitxer.exists()) {
- System.out.println("El fichero no existe");
- } else {
- System.out.println("El fichero existe, desea borrarlo?");
- String respuesta = sc.nextLine();
- if (respuesta.toLowerCase().equals("si")) {
- encontrado = fitxer.delete();
- }
- }
- if (encontrado == false) {
- System.out.println("El fichero no ha sido borrado");
- }
- } else if (comanda.equals("HASH")) {
- String nomFitxer = parts[1];
- String rutaFitxer = directoriTreball.toString() + separadorDirectoris + nomFitxer;
- MessageDigest md = MessageDigest.getInstance("MD5");
- //
- FileInputStream streamFitxer = new FileInputStream(rutaFitxer);
- //
- boolean fiFitxer = false;
- //
- byte[] bloc = new byte[tamanyBlocHash];
- //
- while (!fiFitxer) {
- int llegits = streamFitxer.read(bloc);
- //
- if (llegits >= 0) {
- md.update(bloc, 0, llegits);
- } else {
- fiFitxer = true;
- }
- //
- }
- //
- byte[] hash = md.digest();
- //
- String textHash = byteArrayToHexString(hash);
- //
- streamFitxer.close();
- //
- o.println("Resultat: " + textHash);
- } else if (comanda.equals("ENCRIPTAR")) {
- String nomFitxer1 = parts[1];
- String nomFitxer2 = nomFitxer1 + extensioXifrat;
- String rutaFitxer1 = directoriTreball.toString() + separadorDirectoris + nomFitxer1;
- String rutaFitxer2 = directoriTreball.toString() + separadorDirectoris + nomFitxer2;
- //
- o.print("Escriu una contrasenya [16, 24 o 32 caracters]: ");
- //
- String textClau = i.readLine();
- //
- SecretKeySpec clau = new SecretKeySpec(textClau.getBytes(), "AES");
- //
- Cipher xifrador = Cipher.getInstance("AES/ECB/PKCS5Padding");
- //
- FileInputStream streamFitxer1 = new FileInputStream(rutaFitxer1);
- FileOutputStream streamFitxer2 = new FileOutputStream(rutaFitxer2);
- //
- xifrador.init(Cipher.ENCRYPT_MODE, clau);
- //
- boolean fiFitxer = false;
- int llegits;
- //
- byte[] blocClar = new byte[tamanyBlocEncriptacio];
- //
- while (!fiFitxer) {
- llegits = streamFitxer1.read(blocClar);
- //
- if (llegits >= 0) {
- byte[] blocXifrat = xifrador.update(blocClar, 0, llegits);
- streamFitxer2.write(blocXifrat);
- } else {
- fiFitxer = true;
- }
- }
- //
- streamFitxer1.close();
- streamFitxer2.close();
- //
- o.println("Fitxer encriptat correctament");
- } else if (comanda.equals("DESENCRIPTAR")) {
- String nomFitxer1 = parts[1];
- String nomFitxer2 = nomFitxer1.substring(0, nomFitxer1.length() - extensioXifrat.length());
- String rutaFitxer1 = directoriTreball.toString() + separadorDirectoris + nomFitxer1;
- String rutaFitxer2 = directoriTreball.toString() + separadorDirectoris + nomFitxer2;
- o.print("Escriu la contrasenya: ");
- //
- String textClau = i.readLine();
- //
- SecretKeySpec clau = new SecretKeySpec(textClau.getBytes(), "AES");
- //
- Cipher xifrador = Cipher.getInstance("AES/ECB/PKCS5Padding");
- //
- FileInputStream streamFitxer1 = new FileInputStream(rutaFitxer1);
- FileOutputStream streamFitxer2 = new FileOutputStream(rutaFitxer2);
- //
- xifrador.init(Cipher.DECRYPT_MODE, clau);
- //
- boolean fiFitxer = false;
- int llegits;
- //
- byte[] blocXifrat = new byte[tamanyBlocEncriptacio];
- //
- while (!fiFitxer) {
- llegits = streamFitxer1.read(blocXifrat);
- //
- if (llegits >= 0) {
- byte[] blocClar = xifrador.update(blocXifrat, 0, llegits);
- streamFitxer2.write(blocClar);
- } else {
- fiFitxer = true;
- }
- }
- //
- streamFitxer1.close();
- streamFitxer2.close();
- //
- //
- o.println("Fitxer desencriptat correctament");
- } else if (comanda.equals("GENERARCLAUS_ASIMETRIC")) {
- String nomFitxerClauPublica = parts[1];
- String nomFitxerClauPrivada = parts[2];
- String rutaFitxerClauPublica = directoriTreball.toString() + separadorDirectoris + nomFitxerClauPublica;
- String rutaFitxerClauPrivada = directoriTreball.toString() + separadorDirectoris + nomFitxerClauPrivada;
- FileOutputStream streamFitxer1 = new FileOutputStream(rutaFitxerClauPublica);
- FileOutputStream streamFitxer2 = new FileOutputStream(rutaFitxerClauPrivada);
- // // Generem la clau publica i la privada
- //
- KeyPairGenerator generador = KeyPairGenerator.getInstance("RSA");
- //
- generador.initialize(1024);
- //
- KeyPair pair = generador.generateKeyPair();
- //
- PublicKey clauPublica = pair.getPublic();
- PrivateKey clauPrivada = pair.getPrivate();
- //
- byte[] bytesClauPublica = clauPublica.getEncoded();
- byte[] bytesClauPrivada = clauPrivada.getEncoded();
- //
- // // Escribim els bytes de cada clau als fitxers corresponents
- //
- streamFitxer1.write(bytesClauPublica);
- streamFitxer2.write(bytesClauPrivada);
- //
- //
- o.println("Generada clau publica i privada");
- } else if (comanda.equals("ENCRIPTA_ASIMETRIC")) {
- String nomFitxerClar = parts[1];
- String nomFitxerClauPublica = parts[2];
- String rutaFitxerClar = directoriTreball.toString() + separadorDirectoris + nomFitxerClar;
- String rutaFitxerClauPublica = directoriTreball.toString() + separadorDirectoris + nomFitxerClauPublica;
- String rutaFitxerXifrat = directoriTreball.toString() + separadorDirectoris + nomFitxerClar + extensioXifratAsimetric;
- // // Obtenim els bytes de la clau publica del fitxer corresponent
- //
- File fitxerClauPublica = new File(rutaFitxerClauPublica);
- long tamanyFitxerClauPublica = fitxerClauPublica.length();
- //
- FileInputStream streamClauPublica = new FileInputStream(rutaFitxerClauPublica);
- //
- byte[] bytesClauPublica = new byte[(int) tamanyFitxerClauPublica];
- //
- boolean fiFitxer = false;
- int llegits = streamClauPublica.read(bytesClauPublica);
- //
- while (!fiFitxer) {
- if (llegits >= 0) {
- llegits = streamClauPublica.read(bytesClauPublica, llegits, (int) tamanyFitxerClauPublica - llegits);
- } else {
- fiFitxer = true;
- }
- //
- }
- //
- streamClauPublica.close();
- //
- // // Creem la clau publica a partir dels bytes
- //
- X509EncodedKeySpec especificacioClau = new X509EncodedKeySpec(bytesClauPublica);
- //
- KeyFactory fabricaClaus = KeyFactory.getInstance("RSA");
- //
- PublicKey clauPublica = fabricaClaus.generatePublic(especificacioClau);
- //
- // // Xifrem el fitxer clar
- //
- Cipher xifrador = Cipher.getInstance("RSA/ECB/PKCS1Padding");
- //
- xifrador.init(Cipher.PUBLIC_KEY, clauPublica);
- //
- FileInputStream streamFitxerClar = new FileInputStream(rutaFitxerClar);
- FileOutputStream streamFitxerXifrat = new FileOutputStream(rutaFitxerXifrat);
- //
- byte[] blocClar = new byte[tamanyBlocEncriptacioAsimetrica];
- fiFitxer = false;
- llegits = streamFitxerClar.read(blocClar);
- //
- while (!fiFitxer) {
- if (llegits >= 0) {
- byte[] blocXifrat = xifrador.update(blocClar, 0, llegits);
- streamFitxerXifrat.write(blocXifrat);
- llegits = streamFitxerClar.read(blocClar);
- } else {
- fiFitxer = true;
- }
- //
- }
- //
- byte[] bytesXifrats = xifrador.doFinal();
- streamFitxerXifrat.write(bytesXifrats);
- //
- streamFitxerClar.close();
- streamFitxerXifrat.close();
- o.println("Fitxer encriptat correctament de forma asimetrica");
- } else if (comanda.equals("DESENCRIPTA_ASIMETRIC")) {
- String nomFitxerXifrat = parts[1];
- String nomFitxerClauPrivada = parts[2];
- String nomFitxerPrivat = nomFitxerXifrat.substring(0, nomFitxerXifrat.length() - extensioXifratAsimetric.length());
- String rutaFitxerXifrat = directoriTreball.toString() + separadorDirectoris + nomFitxerPrivat;
- String rutaFitxerClauPublica = directoriTreball.toString() + separadorDirectoris + nomFitxerClauPrivada;
- String rutaFitxerClar = directoriTreball.toString() + separadorDirectoris + nomFitxerXifrat;
- // Adapteu el codi de l'encriptat
- // // Obtenim els bytes de la clau publica del fitxer corresponent
- //
- File fitxerClauPrivada = new File(rutaFitxerClauPublica);
- long tamanyFitxerClauPrivada = fitxerClauPrivada.length();
- //
- FileInputStream streamClauPrivada = new FileInputStream(rutaFitxerClauPublica);
- //
- byte[] bytesClauPrivada = new byte[(int) tamanyFitxerClauPrivada];
- //
- boolean fiFitxer = false;
- int llegits = streamClauPrivada.read(bytesClauPrivada);
- //
- while (!fiFitxer) {
- if (llegits >= 0) {
- llegits = streamClauPrivada.read(bytesClauPrivada, llegits, (int) tamanyFitxerClauPrivada - llegits);
- } else {
- fiFitxer = true;
- }
- //
- }
- //
- streamClauPrivada.close();
- //
- // // Creem la clau publica a partir dels bytes
- //
- X509EncodedKeySpec especificacioClau = new X509EncodedKeySpec(bytesClauPrivada);
- //
- KeyFactory fabricaClaus = KeyFactory.getInstance("RSA");
- //
- PublicKey clauPrivada = fabricaClaus.generatePublic(especificacioClau);
- //
- // // Xifrem el fitxer clar
- //
- Cipher xifrador = Cipher.getInstance("RSA/ECB/PKCS1Padding");
- //
- xifrador.init(Cipher.PRIVATE_KEY, clauPrivada);
- //
- FileInputStream streamFitxerClar = new FileInputStream(rutaFitxerClar);
- FileOutputStream streamFitxerXifrat = new FileOutputStream(rutaFitxerXifrat);
- //
- byte[] blocXifrat = new byte[tamanyBlocEncriptacioAsimetrica];
- fiFitxer = false;
- llegits = streamFitxerClar.read(blocXifrat);
- //
- while (!fiFitxer) {
- if (llegits >= 0) {
- byte[] blocClar = xifrador.update(blocXifrat, 0, llegits);
- streamFitxerXifrat.write(blocXifrat);
- llegits = streamFitxerClar.read(blocXifrat);
- } else {
- fiFitxer = true;
- }
- //
- }
- //
- byte[] bytesXifrats = xifrador.doFinal();
- streamFitxerXifrat.write(bytesXifrats);
- //
- streamFitxerClar.close();
- streamFitxerXifrat.close();
- o.println("Fitxer desencriptat correctament de forma asimetrica");
- } // Altres comandes
- else if (comanda.equals("ENVIAR")) {
- String nomFitxer = parts[1];
- String nomServidor = parts[2];
- String rutaFitxer = directoriTreball.toString() + separadorDirectoris + nomFitxer;
- Socket socketClient = new Socket(nomServidor, portEnviament);
- //
- OutputStream streamEnviar = socketClient.getOutputStream();
- //
- FileInputStream streamFitxer = new FileInputStream(rutaFitxer);
- byte[] bloc = new byte[tamanyBlocEnviament];
- int llegits = streamFitxer.read(bloc);
- boolean fiFitxer = false;
- // // Llegir blocs del fitxer i escriure'ls al stream d'enviament
- // // fins que s'arribi al final del fitxer
- while (!fiFitxer) {
- streamEnviar.write(bloc, 0, llegits);
- llegits = streamFitxer.read(bloc);
- // ...
- }
- streamFitxer.close();
- socketClient.close();
- } else if (comanda.equals("REBRE")) {
- String nomFitxer = parts[1];
- String rutaFitxer = directoriTreball.toString() + separadorDirectoris + nomFitxer;
- ServerSocket servidor = new ServerSocket(63);
- Socket socketServidor = servidor.accept();
- InputStream streamRebre = socketServidor.getInputStream();
- FileOutputStream streamFitxer = new FileOutputStream(rutaFitxer);
- byte[] bloc = new byte[tamanyBlocEnviament];
- int llegits = streamRebre.read(bloc);
- boolean fiRecepcio = false;
- // // Llegir blocs del stream de recepcio i escriure'ls al fitxer
- // // fins que s'arribi al final del stream de recepcio
- while (!fiRecepcio) {
- if (llegits >= 0) {
- streamFitxer.write(bloc, 0, llegits);
- llegits = streamRebre.read(bloc);
- } else {
- fiRecepcio = true;
- }
- }
- streamFitxer.close();
- socketServidor.close();
- servidor.close();
- } else if (comanda.equals("EXECUTAR")) {
- String nomFitxerExecutable = parts[1];
- String rutaFitxerExecutable = directoriTreball.toString() + separadorDirectoris + nomFitxerExecutable;
- Runtime runtime = Runtime.getRuntime();
- Process proces = runtime.exec(rutaFitxerExecutable);
- //
- int codiSortida = proces.waitFor();
- //
- o.println("Programa terminat amb codi: " + codiSortida);
- } else if (comanda.equals("EXECUTAR_AMB_PARAMETRES")) {
- String nomFitxerExecutable = parts[1];
- String rutaFitxerExecutable = directoriTreball.toString() + separadorDirectoris + nomFitxerExecutable;
- //// Construim la llista de parametres
- //
- ArrayList<String> arguments = new ArrayList();
- //
- arguments.add(rutaFitxerExecutable);
- //
- for (int i2 = 2; i2 < parts.length; i2++) {
- arguments.add(parts[i2]);
- }
- //
- ProcessBuilder pb = new ProcessBuilder();
- //
- //// Establim el directori de treball del proces
- //
- File f = directoriTreball.toFile();
- pb.directory(f);
- //
- //// Configurem els arguments i el nom del arxiu executable del proces
- //
- pb.command(arguments);
- //
- Process proces = pb.start();
- //
- //// Iniciem el proces
- //
- int codiSortida = proces.waitFor();
- //
- o.println("Programa terminat amb codi: " + codiSortida);
- //
- } else if (comanda.equals("EXECUTAR_AMB_REDIRECCIO")) {
- String nomFitxerExecutable = parts[1];
- String nomFitxerEntrada = parts[2];
- String nomFitxerSortida = parts[3];
- String rutaFitxerExecutable = directoriTreball.toString() + separadorDirectoris + nomFitxerExecutable;
- String rutaFitxerEntrada = directoriTreball.toString() + separadorDirectoris + nomFitxerEntrada;
- String rutaFitxerSortida = directoriTreball.toString() + separadorDirectoris + nomFitxerSortida;
- File e = new File(rutaFitxerEntrada);
- File s = new File(rutaFitxerSortida);
- //
- ProcessBuilder pb = new ProcessBuilder();
- //
- File f = directoriTreball.toFile();
- pb.directory(f);
- //
- pb.redirectInput(e);
- pb.redirectOutput(s);
- //
- pb.command(rutaFitxerExecutable);
- //
- Process proces = pb.start();
- //
- int codiSortida = proces.waitFor();
- //
- o.println("Programa terminat amb codi: " + codiSortida);
- } else if (comanda.equals("EXECUTAR_SEGON_PLA")) {
- String nomFitxerExecutable = parts[1];
- String rutaFitxerExecutable = directoriTreball.toString() + separadorDirectoris + nomFitxerExecutable;
- boolean error = false;
- //// Control d'errors
- //
- if (process != null) {
- if (process.isAlive()) {
- o.println("ERROR: Cal detenir el procés en segon pla iniciat prèviament");
- error = true;
- }
- //
- }
- //
- if (!error) {
- ProcessBuilder pb = new ProcessBuilder();
- //
- File f = directoriTreball.toFile();
- pb.directory(f);
- //
- pb.command(rutaFitxerExecutable);
- //
- process = pb.start();
- //
- o.println("Execucio en segon pla iniciada");
- //
- }
- } else if (comanda.equals("DETENIR_SEGON_PLA")) {
- boolean error = false;
- //// Control errors
- //
- if (process == null) {
- o.println("ERROR: No hi ha cap procés executant-se en segon pla");
- error = true;
- }
- //
- if (!error) {
- //
- if (!process.isAlive()) {
- o.println("El procés en segon pla ja estava detingut");
- } else {
- process.destroy();
- o.println("Esperant finalització");
- int codigo = process.waitFor();
- o.println("Procés finalitzat");
- //
- }
- process = null;
- }
- } // Altres comandes
- else if (comanda.equals("COMPRIMIR")) {
- // long tempsInici = ...;
- String nomFitxer = parts[1];
- String nomFitxerComprimit = nomFitxer + extensioCompressio;
- String rutaFitxer = directoriTreball.toString() + separadorDirectoris + nomFitxer;
- String rutaFitxerComprimit = directoriTreball.toString() + separadorDirectoris + nomFitxerComprimit;
- // Obrim fitxer zip
- FileOutputStream fos = new FileOutputStream(rutaFitxerComprimit);
- ZipOutputStream zos = new ZipOutputStream(fos);
- // Creem entrada fitxer zip
- ZipEntry ze = new ZipEntry(nomFitxer);
- zos.putNextEntry(ze);
- // Passem les dades del fitxer d'entrada al zip
- FileInputStream fis = new FileInputStream(rutaFitxer);
- byte[] bloc = new byte[tamanyBlocCompressio];
- int llegits = fis.read(bloc);
- while (llegits >= 0) {
- zos.write(bloc, 0, llegits);
- llegits = fis.read(bloc);
- }
- fis.close();
- // Tanquem l'entrada
- zos.closeEntry();
- // Tanquem el fitxer zip
- zos.close();
- System.out.println(nomFitxer + " comprimit");
- // long tempsUtilitzat = ...;
- System.out.println("---------------------");
- System.out.println("Compressio completada");
- // System.out.println("Temps utilitzat: " + + tempsUtilitzat / 1000.0 + " segons");
- } else if (comanda.equals("COMPRIMIR_SEQUENCIAL")) {
- // long tempsInici = ...;
- //for(int i2 = ...; i2 < ...; i2 ++)
- //{
- // String nomFitxer = ...;
- //
- // ...
- //
- // System.out.println(nomFitxer + " comprimit");
- //
- //}
- // long tempsUtilitzat = ...;
- System.out.println("---------------------");
- System.out.println("Compressio completada");
- // System.out.println("Temps utilitzat: " + + tempsUtilitzat / 1000.0 + " segons");
- } else if (comanda.equals("COMPRIMIR_PARALEL")) {
- // long tempsInici = ...;
- // Thread []fils = new Thread[...];
- //
- //// Creem els fils
- //
- //for(int i2 = ...; i2 < ...; i2 ++)
- //{
- // String nomFitxer = ...;
- //
- // String nomFitxerComprimit = nomFitxer + extensioCompressio;
- //
- // String rutaFitxer = directoriTreball.toString() + separadorDirectoris + nomFitxer;
- // String rutaFitxerComprimit = directoriTreball.toString() + separadorDirectoris + nomFitxerComprimit;
- //
- // FilCompressio fc = new FilCompressio(...);
- // fils[...] = new Thread(...);
- //
- //}
- //
- //
- //// Iniciem els fils
- //
- //for(int i2 = ...; i2 < ...; i2 ++)
- //{
- // fils[...]....();
- //}
- //
- //// Esperem que els fils acabin
- //
- //for(int i2 = ...; i2 < ...; i2 ++)
- //{
- // fils[...]....();
- //}
- //
- //
- //long tempsUtilitzat = ...;
- //
- //System.out.println("---------------------");
- //System.out.println("Compressio completada");
- //System.out.println("Temps utilitzat: " + + tempsUtilitzat / 1000.0 + " segons");
- } else if (comanda.equals("DESCARREGAR")) {
- String urlEntrada = parts[1];
- String nomFitxerSortida = parts[2];
- String rutaFitxerSortida = directoriTreball.toString() + separadorDirectoris + nomFitxerSortida;
- //// Obrim la url
- //
- URL url = new URL(urlEntrada);
- InputStream streamEntrada = url.openStream();
- //
- OutputStream streamSortida = new FileOutputStream(rutaFitxerSortida);
- //
- byte bloc[] = new byte[tamanyBlocDescarrega];
- //
- int bytesDescarregats = 0;
- int llegits = streamEntrada.read(bloc);
- //
- while (llegits >= 0) {
- streamSortida.write(bloc, 0, llegits);
- //Comptabilitzem els bytes descarregats
- bytesDescarregats++;
- llegits = streamEntrada.read(bloc);
- }
- streamEntrada.close();
- streamSortida.close();
- //
- o.println("Descarrega finalitzada. Bytes descarregats " + bytesDescarregats);
- } else if (comanda.equals("AFEGIR_DESCARREGA")) {
- String urlEntrada = parts[1];
- String nomFitxerSortida = parts[2];
- String rutaFitxerSortida = directoriTreball.toString() + separadorDirectoris + nomFitxerSortida;
- System.out.println(urlEntrada);
- System.out.println(rutaFitxerSortida);
- gd.IniciarDescarrega(urlEntrada, rutaFitxerSortida);
- } else if (comanda.equals("ESTAT_DESCARREGAS")) {
- gd.ImprimirEstatDescarregues();
- } else if (comanda.equals("SORTIR")) {
- fi = true;
- }
- }
- }
- public static Path obtenirDirectoriArrel() {
- Path result;
- Iterable<Path> roots = FileSystems.getDefault().getRootDirectories();
- Iterator<Path> itRoots = roots.iterator();
- if (itRoots.hasNext()) {
- result = itRoots.next();
- } else {
- result = null;
- }
- return result;
- }
- public static String obtenirSeparadorDirectoris() {
- return FileSystems.getDefault().getSeparator();
- }
- static String byteArrayToHexString(byte[] bytes) {
- String r = "";
- for (int i = 0; i < bytes.length; i++) {
- r += Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1);
- }
- return r;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement