Experiment No 16
Experiment No 16
16
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
import java.util.zip.ZipEntry;
// Source Field
JLabel sourceLabel = new JLabel("Source");
gbc.gridx = 0;
gbc.gridy = 0;
mainPanel.add(sourceLabel, gbc);
// Algorithm ComboBox
JLabel algorithmLabel = new JLabel("Algorithms");
gbc.gridx = 0;
gbc.gridy = 2;
mainPanel.add(algorithmLabel, gbc);
String[] algorithms = { "Zip", "RLE", "LZW" };
algorithmComboBox = new JComboBox<>(algorithms);
gbc.gridx = 1;
mainPanel.add(algorithmComboBox, gbc);
// Destination Field
JLabel destinationLabel = new JLabel("Destination");
gbc.gridx = 0;
gbc.gridy = 3;
mainPanel.add(destinationLabel, gbc);
// Output Area
outputArea = new JTextArea(5, 20);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 3;
mainPanel.add(scrollPane, gbc);
gbc.gridwidth = 1;
add(mainPanel, BorderLayout.CENTER);
// Event Listeners
browseSourceButton.addActionListener(e -> selectFile(sourceField));
browseDestinationButton.addActionListener(e ->
selectFile(destinationField));
okButton.addActionListener(e -> handleCompressionOrDecompression());
clearButton.addActionListener(e -> clearFields());
setLocationRelativeTo(null);
}
if (sourcePath.isEmpty() || destinationPath.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please select both source and
destination paths.");
return;
}
if (!sourceFile.exists()) {
JOptionPane.showMessageDialog(this, "Source file does not exist.");
return;
}
if (isCompress) {
outputArea.append("Compressing using " + selectedAlgorithm + "...\n");
try {
if ("Zip".equals(selectedAlgorithm)) {
compressZip(sourceFile, destinationFile);
} else {
outputArea.append("Compression method not implemented.\n");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Compression failed: " +
e.getMessage());
}
} else if (isCompress == false) {
outputArea.append("Decompressing using " + selectedAlgorithm + "...\n");
// Implement decompression logic here based on the selected algorithm.
}
}
Output :
Conclusion :