diff --git a/.DS_Store b/.DS_Store index f967f61..0706256 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.classpath b/.classpath index c0f260f..0cbf9cd 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,6 @@ - - - - - + diff --git a/.project b/.project index 4c6a839..785a2b0 100644 --- a/.project +++ b/.project @@ -1,6 +1,6 @@ - LZW Compression Part 1 + LZW-Compression-Part-1 diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..c06b8a7 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,3 @@ +/LZWCompression.class +/lzw-file1.txt +/Tester.class diff --git a/bin/LZWCompression.class b/bin/LZWCompression.class index 8339b66..6766562 100644 Binary files a/bin/LZWCompression.class and b/bin/LZWCompression.class differ diff --git a/src/LZWCompression.java b/src/LZWCompression.java index 76a869b..ec45896 100644 --- a/src/LZWCompression.java +++ b/src/LZWCompression.java @@ -1,10 +1,14 @@ - import java.util.*; import java.io.*; import java.nio.file.Files; -public class LZWCompression{ +public class LZWCompression +{ + //everything works if you put in an ArrayList but you can't feed in a file yet HashMap hMap = new HashMap(); + ArrayList theArrList = new ArrayList(); + int [] intArray = {64, 256, 257, 258, 259, 260, 261, 261, 38, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 273, 262, 280, 281, 261, 13, 10, 282, 286, 286, 38, 44, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 298, 287, 306, 283, 285, 307, 310, 257, 289, 44, 42, 35, 316, 317, 318, 319, 320, 321, 322, 323, 324, 321, 38, 325, 328, 329, 330, 324, 313, 44, 311, 311, 284, 335, 311, 313, 315, 331, 342, 343, 332, 291, 47, 344, 348, 349, 316, 333, 338, 307, 337, 353, 306, 340, 350, 359, 330, 333, 292, 327, 360, 365, 323, 352, 356, 286, 355, 369, 282, 358, 342, 37, 32, 32, 364, 366, 326, 299, 379, 380, 320, 377, 383, 329, 368, 372, 280, 371, 390, 262, 374, 361, 377, 377, 46, 384, 318, 362, 382, 400, 317, 38, 397, 377, 342, 389, 393, 260, 392, 411, 259, 395, 329, 407, 386, 404, 40, 295, 265, 296, 387, 365, 418, 378, 331, 410, 414, 257, 413, 431, 256, 416, 325, 376, 418, 37, 384, 402, 290, 269, 295, 42, 440, 380, 427, 409, 291, 434, 259, 433, 434, 436, 325, 427, 425, 348, 442, 44, 37, 272, 422, 448, 418, 459, 322, 430, 434, 454, 431, 456, 35, 438, 377, 341, 35, 40, 418, 317, 476, 428, 361, 424, 275, 465, 330, 483, 478, 483, 32, 405, 407, 459, 470, 431, 472, 414, 456, 427, 397, 327, 495, 35, 502, 419, 345, 422, 278, 37, 488, 324, 507, 428, 505, 515, 387, 497, 414, 499, 411, 501, 515, 446, 377, 40, 406, 518, 324, 421, 294, 278, 267, 42, 513, 321, 515, 32, 462, 377, 47, 492, 467, 319, 520, 411, 522, 393, 474, 46, 447, 318, 42, 32, 463, 556, 377, 553, 317, 556, 367, 510, 534, 268, 296, 560, 319, 552, 320, 480, 35, 406, 438, 568, 506, 576, 547, 393, 549, 390, 474, 323, 47, 493, 506, 573, 450, 294, 463, 565, 271, 295, 576, 325, 585, 316, 493, 468, 381, 290, 452, 258, 581, 372, 583, 323, 493, 376, 595, 469, 445, 592, 487, 533, 342, 609, 562, 429, 451, 603, 256, 605, 369, 607, 320, 555, 289, 493, 559, 325, 461, 44, 614, 277, 512, 616, 596, 378, 44, 630, 619, 328, 579, 390, 624, 356, 626, 318, 631, 397, 506, 484, 320, 37, 47, 564, 635, 277, 536, 638, 538, 556, 529, 493, 46, 654, 509, 334, 622, 64, 647, 353, 649, 655, 477, 632, 658, 659, 534, 424, 332, 527, 343, 645, 372, 673, 338, 675, 344, 461, 591, 680, 635, 295, 404, 351, 621, 622, 688, 335, 690, 620, 294, 42, 694, 707, 461, 600, 460, 699, 603, 701, 339, 290, 478, 360, 633, 636, 528, 270, 641, 540, 267, 591, 656, 275, 637, 293, 710, 589, 670, 700, 309, 671, 703, 324, 656, 679, 487, 377, 723, 290, 507, 744, 540, 742, 660, 537, 349, 686, 369, 714, 310, 703, 265, 568, 264, 634, 363, 614, 723, 539, 745, 274, 641, 46, 423, 291, 42, 47, 772, 731, 678, 602, 671, 754, 307, 756, 533, 657, 333, 38, 693, 762, 304, 761, 593, 397, 292, 539, 314, 774, 546, 712, 452, 778, 357, 716, 620, 418, 44, 421, 790, 565, 35, 293, 790, 763, 407, 46, 743, 427, 634, 266, 573, 759, 764, 765, 704, 733, 713, 735, 622, 690, 693, 667, 539, 785, 766, 502, 294, 407, 791, 747, 815, 268, 667, 291, 832, 266, 44, 573, 810, 724, 38, 657, 462, 468, 752, 356, 798, 287, 607, 362, 819, 843, 831, 748, 37, 828, 812, 44, 828, 745, 46, 812, 742, 748, 566, 792, 842, 38, 661, 629, 790, 805, 563, 776, 734, 671, 435, 800, 469, 264, 804, 857, 273, 810, 817, 842, 835, 832, 746, 32, 865, 870, 267, 845, 742, 840, 815, 443, 900, 634, 669, 882, 853, 288, 884, 345, 327, 900, 418, 552, 273, 848, 811, 292, 867, 895, 869, 540, 845, 849, 722, 814, 900, 785, 536, 814, 37, 42, 446, 601, 822, 797, 824, 603, 626, 783, 845, 897, 834, 681, 943, 744, 810, 949, 763, 290, 532, 842, 841, 291, 830, 818, 841, 612, 880, 823, 882, 64, 583, 289, 848, 291, 828, 793, 914, 742, 47, 267, 887, 819, 923, 923, 951, 747, 418, 706, 423, 397, 868, 641, 837, 927, 814, 264, 960, 937, 471, 939, 452, 456, 933, 293, 42, 721, 899, 977, 814, 982, 913, 809, 810, 863, 746, 864, 809, 869, 865, 987, 954, 901, 978, 269, 653, 802, 362, 425, 851, 353, 909, 373, 911, 316, 729, 788, 954, 314, 480, 857, 864, 863, 814, 978, 877, 767, 1002, 805, 265, 887, 443, 958, 980, 901, 42, 944, 834, 532, 291, 383, 1023, 338, 1025, 281, 395, 709, 889, 896, 922, 802, 834, 427, 868, 398, 1064, 1068, 951, 876, 398, 1045, 270, 555, 984, 925, 949, 769, 998, 447, 1055, 335, 1057, 280, 358, 461, 1004, 859, 314, 925, 877, 1011, 1069, 977, 979, 397, 1000, 957, 464, 905, 1071, 444, 590, 844, 961, 938, 963, 633, 1053, 592, 1092, 1041, 895, 813, 869, 743, 1097, 1042, 728, 1102, 949, 839, 593, 705, 796, 993, 1109, 297, 659, 35, 934, 931, 836, 1069, 1009, 290, 867, 1093, 1037, 945, 859, 742, 891, 267, 807, 495, 270, 967, 294, 908, 994, 455, 679, 808, 1120, 444, 764, 905, 515, 950, 1142, 276, 810, 1000, 464, 898, 1104, 1151, 777, 1153, 473, 730, 789, 1163, 266, 1134, 1161, 1141, 1117, 1039, 981, 1010, 1157, 421, 667, 812, 528, 483, 760, 802, 1089, 818, 1014, 1169, 1127, 498, 1172, 500, 346, 1080, 1113, 742, 40, 661, 976, 977, 839, 980, 979, 1203, 1168, 265, 1076, 857, 1020, 269, 782, 277, 292, 1152, 1129, 1074, 265, 1019, 1207, 1207, 949, 877, 1013, 792, 1166, 899, 812, 266, 975, 841, 708, 759, 1107, 1128, 882, 783, 269, 37, 1186, 1227, 1119, 1119, 915, 971, 1159, 1233, 1146, 1252, 559, 1239, 1240, 47, 1222, 1243, 1111, 892, 832, 802, 951, 1097, 1009, 529, 1067, 1227, 857, 1193, 1256, 1213, 694, 1053, 1261, 882, 848, 899, 791, 1188, 1072, 527, 665, 876, 948, 313, 1194, 769, 1272, 976, 837, 986, 982, 1237, 896, 983, 1157, 278, 1278, 1171, 963, 964, 1263, 421, 1125, 1074, 585, 903, 903, 659, 997, 1184, 1140, 1161, 1143, 922, 1195, 1302, 1241, 1242, 963, 1263, 766, 1321, 1258, 271, 512, 1228, 1143, 811, 659, 1303, 1304, 1305, 1244, 1329, 1125, 764, 1008, 833, 1294, 1333, 1016, 1301, 272, 1052, 1305, 1085, 282, 729, 1340, 1300, 1293, 745, 1227, 399, 1167, 502, 1008, 32, 1254, 722, 443, 1351, 1199, 452, 291, 1145, 1355, 1316, 1357, 1047, 1159, 790, 1365, 1215, 1176, 1100, 1366, 634, 1305, 672, 1369, 1154, 1224, 1329, 1033, 896, 1375, 1376, 1159, 1285, 924, 1063, 1276, 1220, 1367, 1385, 1352, 910, 290, 1372, 1277, 952, 1275, 846, 1046, 1409, 1276, 807, 1050, 540, 1382, 1400, 1384, 1385, 1386, 1420, 312, 1030, 707, 948, 1009, 1238, 268, 1332, 1355, 1309, 38, 1422, 1421, 357, 1433, 393, 807, 1389, 1258, 1045, 971, 1195, 1415, 989, 1356, 1410, 708, 1052, 1437, 606, 1451, 282, 337, 528, 910, 1453, 625, 1326, 592, 1150, 1382, 1445, 1448, 723, 1331, 1394, 1294, 973, 990, 1328, 1327, 1401, 606, 1419, 1058, 47, 337, 313, 573, 281, 1336, 1459, 1460, 615, 840, 1176, 1046, 984, 652, 983, 463, 653, 746, 47, 1209, 1228, 1472, 272, 661, 1476, 1484, 992, 258, 913, 634, 355, 1052, 1502, 1505, 363, 550, 1424, 1418, 1357, 289, 768, 423, 1282, 1240, 1240, 440, 1396, 901, 1417, 889, 952, 1513, 1483, 1511, 291, 40, 1435, 442, 1458, 1306, 293, 463, 582, 1514, 1341, 1357, 1076, 634, 1470, 762, 792, 1499, 1196, 1536, 339, 1029, 1474, 1423, 730, 1435, 64, 1439, 1240, 1474, 1244, 761, 1475, 1485, 566, 1487, 812, 1464, 924, 924, 1518, 275, 1132, 1371, 1517, 745, 1365, 273, 1439, 1554, 702, 1021, 783, 1555, 1442, 44, 1260, 1557, 783, 386, 292, 817, 952, 503, 293, 1456, 674, 1541, 1175, 495, 1079, 1549, 892, 1398, 745, 998, 1486, 858, 674, 1580, 443, 652, 1045, 1509, 529, 1584, 1557, 1537, 634, 495, 1150, 1612, 467, 1509, 1045, 339, 1615, 1527, 1174, 1080, 635, 1206, 1159, 1546, 917, 1512, 755, 442, 528, 458, 294, 973, 1601, 1474, 433, 967, 1380, 1286, 1053, 539, 1560, 1530, 1026, 1599, 913, 1578, 289, 1335, 1229, 503, 1631, 1500, 1563, 799, 1174, 1651, 1053, 1391, 397, 1501, 42, 453, 1153, 1021, 819, 333, 982, 502, 1617, 779, 1596, 1431, 1169, 693, 1103, 1608, 815, 755, 1650, 458, 874, 1690, 1667, 1653, 499, 38, 1052, 1643, 1679, 1546, 486, 1286, 648, 402, 1635, 1683, 1169, 1629, 1653, 779, 1616, 719, 264, 1699, 458, 1671, 1551, 1618, 394, 1509, 557, 1591, 904, 467, 402, 1718, 1582, 1642, 1707, 1164, 1607, 523, 1666, 1334, 1693, 1506, 290, 973, 1454, 1387, 415, 1501, 746, 1535, 665, 503, 825, 633, 1526, 1407, 705, 264, 940, 1691, 533, 901, 770, 1727, 412, 1741, 1086, 1746, 1286, 1743, 290, 1539, 1420, 1652, 299, 347, 785, 479, 303, 265, 1768, 1553, 793, 1669, 386, 1759, 281, 778, 1287, 1445, 768, 1625, 1688, 1434, 266, 1315, 302, 1166, 1434, 964, 1639, 1524, 1679, 1024, 1761, 263, 984, 1287, 764, 463, 533, 1781, 1794, 1808, 415, 1535, 544, 507, 265, 1747, 1084, 1800, 415, 1670, 1763, 378, 784, 819, 528, 1732, 1809, 1826, 312, 1765, 847, 1216, 378, 1780, 1518, 1815, 310, 701, 264, 1651, 503, 386, 1468, 1725, 1538, 1807, 1827, 582, 1553, 1822, 539, 768, 495, 1840, 1832, 1725, 336, 1817, 257, 730, 1648, 408, 407, 573, 1840, 1226, 1679, 1625, 1767, 1846, 606, 1553, 1640, 1676, 1852, 1395, 293, 1856, 674, 442, 973, 1779, 407, 480, 1851, 1780, 1676, 1787, 1753, 1869, 373, 1796, 1293, 1814, 515, 528, 1622, 849, 1056, 1857, 1762, 709, 1881, 1853, 32, 585, 1854, 829, 1692, 590, 1888, 1794, 264, 1777, 793, 1812, 1679, 556, 763, 1812, 1824, 1806, 1845, 1740, 1748, 1909, 427, 1391, 376, 1616, 1227, 264, 1765, 295, 40, 957, 1900, 757, 1787, 1752, 1820, 507, 1722, 1863, 378, 1722, 495, 1777, 1910, 1198, 1768, 1622, 1945, 467, 1148, 1906, 1359, 693, 47, 1932, 787, 1587, 757, 1569, 527, 1050, 1849, 1943, 1820, 784, 293, 1739, 1368, 1809, 1908, 1053, 1860, 634, 1881, 1715, 1955, 1393, 1981, 976, 1838, 1725, 1050, 706, 1825, 1434, 1403, 1728, 292, 706, 1569, 504, 1853, 1984, 861, 1227, 1722, 1940, 829, 664, 1873, 1362, 1745, 1846, 1990, 625, 1535, 1905, 1725, 1746, 665, 1905, 1072, 1984, 458, 1885, 1620, 1854, 504, 1187, 1893, 1872, 1902, 418, 973, 1806, 1889, 432, 1899, 1789, 1879, 1849, 819, 1952, 1841, 2018, 1862, 1616, 628, 2038, 1658, 976, 463, 1932, 1948, 2030, 2031, 2049, 854, 633, 1470, 1865, 1982, 1393, 817, 1896, 1074, 2051, 1782, 2032, 2061, 1471, 302, 1771, 592, 440, 2059, 296, 721, 2061, 1877, 2073, 500, 1790, 1205, 1960, 998, 1934, 266, 2075, 414}; + ArrayList theIntegerArrList = new ArrayList(); public LZWCompression() { @@ -12,6 +16,11 @@ public LZWCompression() { hMap.put("" + (char)(k),k); } + + for (int i = 0; i < intArray.length; i++) + { + theIntegerArrList.add(intArray[i]); + } } public void compression (String str) throws IOException @@ -86,6 +95,62 @@ public byte[] compress (String str) { arr[k] = arrList.get(k); } + + theArrList = arrList; return(arr); } + + public String decompress () throws Exception + { + ArrayList compressed = new ArrayList(); + + for (int i = 0; i < theIntegerArrList.size(); i++) + { + compressed.add(theIntegerArrList.get(i).intValue()); + } + + Map dictionary = new HashMap(); + + int dictionarySize = 256; + + for (int i = 0; i < dictionarySize; i++) + { + Character theChar = (char)i; + String theString = "" + theChar; + dictionary.put(i, theString); + } + + int firstCompressedInt = compressed.get(0); + Character firstCompressedChar = (char)firstCompressedInt; + + compressed.remove(0); + + String currentLetters = "" + firstCompressedChar; + + String output = currentLetters; + + for (int i = 0; i < compressed.size(); i++) + { + int letters = compressed.get(i); + + String dictionaryEntry; + + if (dictionary.containsKey(letters)) + { + dictionaryEntry = dictionary.get(letters); + } + else + { + dictionaryEntry = currentLetters + currentLetters.charAt(0); + } + + output = output + dictionaryEntry; + + dictionary.put(dictionarySize++, currentLetters + dictionaryEntry.charAt(0)); + + currentLetters = dictionaryEntry; + } + + return (output); + } } \ No newline at end of file diff --git a/src/Tester.java b/src/Tester.java new file mode 100644 index 0000000..10bfcff --- /dev/null +++ b/src/Tester.java @@ -0,0 +1,26 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; + +public class Tester +{ + public static void main(String[] args) throws Exception + { + LZWCompression compressor = new LZWCompression (); + + +// BufferedReader reader = new BufferedReader(new FileReader("lzw-file1.txt")); +// +// String str = null; +// +// while (reader.ready()) +// { +// str = str + reader.read(); +// } +// +// compressor.fileCompression (str); +// compressor.compress(str); + + System.out.println(compressor.decompress()); + } +}