Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class ZipUnzip {
- public static void main(String[] args) {
- userInterface();
- }
- public static void userInterface() {
- Scanner console = new Scanner(System.in);
- System.out.print("Would you like to compress or decompress a file? : ");
- String pressAnswer = console.next();
- boolean Answer = false;
- if (pressAnswer.equals("compress")) {
- Answer = true;
- }
- else if (pressAnswer.equals("decompress")) {
- Answer = false;
- }
- else {
- System.out.println("Invalid command");
- System.exit(1);
- }
- System.out.print("What will be the name of the input file?: ");
- Scanner in = getInputScanner(console);
- System.out.print("What will be the name of the output file?: ");
- PrintStream out = getOutputPrintStream(console);
- processFile( Answer, in , out);
- }
- public static Scanner getInputScanner(Scanner console){
- Scanner in = null; //null signifies NO object reference
- while (in == null) {
- System.out.print("input file name? ");
- String name = console.next();
- try {
- in = new Scanner(new File(name));
- }
- catch (FileNotFoundException e) {
- System.out.println("File not found. "
- + "Please try again.");
- }
- }
- return in;
- }
- public static PrintStream getOutputPrintStream(Scanner console){
- String outfilename = console.next();
- File f = new File(outfilename);
- if (f.exists()) {
- System.out.println("The file exists.... exiting");
- System.exit(1);
- }
- PrintStream out = null;
- try {
- out = new PrintStream(f);
- }
- catch (FileNotFoundException e) {
- System.out.println(e.getMessage());
- System.exit(1);
- }
- return out;
- }
- public static void processFile (boolean compress, Scanner input, PrintStream output){
- if(compress == true) {
- while (input.hasNextLine()) {
- String line = input.nextLine();
- output.println(compressLine(line));
- }
- }
- else {
- while (input.hasNextLine()) {
- String line = input.nextLine();
- output.println(decompressLine(line));
- }
- }
- }
- public static String compressLine(String line){
- Scanner scan = new Scanner(line);
- String compressedLine = "";
- String compressedWord = "";
- while(scan.hasNext()) {
- String word = scan.next();
- compressedWord = compress(word);
- compressedLine = compressedLine + " " + compressedWord;
- }
- return compressedLine;
- }
- public static String compress(String word) {
- String compressedWord = word;
- if (word.length() < 2) {
- word = compressedWord;
- }
- else if (word.length() == 2) {
- if (word == "an") {
- compressedWord = "%";
- }
- else if (word == "re") {
- compressedWord = "@";
- }
- else {
- compressedWord = word;
- }
- }
- else if (word.length() > 2) {
- for (int i = 2; i < compressedWord.length(); i++) {
- String search = compressedWord.substring(i-2, i+1);
- if (search.equals("the")) {
- System.out.println("FLAG the");
- compressedWord = compressedWord.substring(0, i-2) + "~" + compressedWord.substring(i+1, compressedWord.length());
- break;
- }
- else {
- continue;
- }
- }
- for (int i = 2; i < compressedWord.length(); i++) {
- String search = compressedWord.substring(i-2, i+1);
- if (search.equals("ion")) {
- compressedWord = compressedWord.substring(0, i-2) + "&" + compressedWord.substring(i+1, compressedWord.length());
- break;
- }
- else {
- continue;
- }
- }
- for (int i = 2; i < word.length(); i++) {
- String search = word.substring(i-2, i+1);
- if (search.equals("ing")) {
- compressedWord = compressedWord.substring(0, i-2) + "#" + compressedWord.substring(i+1, compressedWord.length());
- break;
- }
- else {
- continue;
- }
- }
- for (int i = 1; i < compressedWord.length(); i++) {
- String search = compressedWord.substring(i-1, i+1);
- if (search.equals("an")) {
- compressedWord = compressedWord.substring(0, i-1) + "%" + compressedWord.substring(i+1, compressedWord.length());
- break;
- }
- else {
- continue;
- }
- }
- for (int i = 1; i < compressedWord.length(); i++) {
- String search = compressedWord.substring(i-1, i+1);
- if (search.equals("re")) {
- compressedWord = compressedWord.substring(0, i-1) + "@" + compressedWord.substring(i+1, compressedWord.length());
- break;
- }
- else {
- continue;
- }
- }
- for (int i = 2; i < compressedWord.length(); i++) {
- String search = compressedWord.substring(i-2, i+1);
- if (search.equals("con")) {
- compressedWord = compressedWord.substring(0, i-2) + "$" + compressedWord.substring(i+1, word.length());
- break;
- }
- else {
- continue;
- }
- }
- }
- return compressedWord;
- }
- public static String decompressLine(String line){
- Scanner scan = new Scanner(line);
- String decompressedLine = "";
- String decompressedWord = "";
- while(scan.hasNext()) {
- String word = scan.next();
- decompressedWord = decompress(word);
- decompressedLine = decompressedLine + " " + decompressedWord;
- }
- return decompressedLine;
- }
- public static String decompress(String word) {
- String decompressedWord = word;
- System.out.println(decompressedWord);
- if (decompressedWord.length() == 1) {
- if (decompressedWord.equals("~")) {
- decompressedWord = "the";
- }
- else if (decompressedWord.equals("&")) {
- decompressedWord = "ion";
- }
- else if (decompressedWord.equals("#")) {
- decompressedWord = "ing";
- }
- else if (decompressedWord.equals("%")) {
- decompressedWord = "an";
- }
- else if (decompressedWord.equals("@")) {
- decompressedWord = "re";
- }
- else if (decompressedWord.equals("$")) {
- decompressedWord = "con";
- }
- }
- else if (decompressedWord.length() > 1) {
- for (int i = 0; i < decompressedWord.length() ; i++) {
- char search = decompressedWord.charAt(i);
- if (search == '~') {
- decompressedWord = decompressedWord.substring(0, i) +"the" + decompressedWord.substring(i+1, decompressedWord.length());
- }
- }
- for (int i = 0; i < decompressedWord.length(); i++) {
- char search = decompressedWord.charAt(i);
- if (search == '&') {
- decompressedWord = decompressedWord.substring(0, i) +"ion" + decompressedWord.substring(i+1, decompressedWord.length());
- }
- }
- for (int i = 0; i < decompressedWord.length(); i++) {
- char search = decompressedWord.charAt(i);
- if (search == '#') {
- decompressedWord = decompressedWord.substring(0, i) +"ing" + decompressedWord.substring(i+1, decompressedWord.length());
- }
- }
- for (int i = 0; i < decompressedWord.length(); i++) {
- char search = decompressedWord.charAt(i);
- if (search == '%') {
- decompressedWord = decompressedWord.substring(0, i) +"an" + decompressedWord.substring(i+1, decompressedWord.length());
- }
- }
- for (int i = 0; i < decompressedWord.length(); i++) {
- char search = decompressedWord.charAt(i);
- if (search == '@') {
- decompressedWord = decompressedWord.substring(0, i) +"re" + decompressedWord.substring(i+1, decompressedWord.length());
- }
- }
- for (int i = 0; i < decompressedWord.length(); i++) {
- char search = decompressedWord.charAt(i);
- if (search == '$') {
- decompressedWord = decompressedWord.substring(0, i) +"con" + decompressedWord.substring(i+1, decompressedWord.length());
- }
- }
- }
- return decompressedWord;
- }
- }
Add Comment
Please, Sign In to add comment