0% found this document useful (0 votes)
5 views9 pages

Experiment No 16

Uploaded by

samikshapatil775
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Experiment No 16

Uploaded by

samikshapatil775
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

EXPERIMENT NO.

16

Name: Snehal Santram Jadhav


Class: CSE(AIML)
Batch: S6
Rollno: 153

1)Problem Statement : Design and develop the mini project for


solving the different real time problems using java language in the
group of 4-5 students

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
import java.util.zip.ZipEntry;

public class FileCompressorApp extends JFrame {


private JTextField sourceField, destinationField;
private JButton browseSourceButton, browseDestinationButton, okButton,
clearButton;
private JRadioButton compressRadioButton, decompressRadioButton;
private JComboBox<String> algorithmComboBox;
private JTextArea outputArea;
private JFileChooser fileChooser;
public FileCompressorApp() {
setTitle("Compression-Decompression");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

// Main panel with GridBagLayout


JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBackground(new Color(255, 255, 204));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);

// Source Field
JLabel sourceLabel = new JLabel("Source");
gbc.gridx = 0;
gbc.gridy = 0;
mainPanel.add(sourceLabel, gbc);

sourceField = new JTextField(20);


gbc.gridx = 1;
mainPanel.add(sourceField, gbc);

browseSourceButton = new JButton("Browse");


gbc.gridx = 2;
mainPanel.add(browseSourceButton, gbc);
// Procedure Radio Buttons
JLabel procedureLabel = new JLabel("Procedure");
gbc.gridx = 0;
gbc.gridy = 1;
mainPanel.add(procedureLabel, gbc);

compressRadioButton = new JRadioButton("Compress");


decompressRadioButton = new JRadioButton("Decompress");
ButtonGroup procedureGroup = new ButtonGroup();
procedureGroup.add(compressRadioButton);
procedureGroup.add(decompressRadioButton);

JPanel procedurePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));


procedurePanel.add(compressRadioButton);
procedurePanel.add(decompressRadioButton);
gbc.gridx = 1;
gbc.gridwidth = 2;
mainPanel.add(procedurePanel, gbc);
gbc.gridwidth = 1;

// 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);

destinationField = new JTextField(20);


gbc.gridx = 1;
mainPanel.add(destinationField, gbc);

browseDestinationButton = new JButton("Browse");


gbc.gridx = 2;
mainPanel.add(browseDestinationButton, 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;

// Ok and Clear Buttons


okButton = new JButton("Ok");
clearButton = new JButton("Clear");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(okButton);
buttonPanel.add(clearButton);
gbc.gridx = 1;
gbc.gridy = 5;
gbc.gridwidth = 2;
mainPanel.add(buttonPanel, gbc);

add(mainPanel, BorderLayout.CENTER);

// File chooser for selecting files and directories (files only)


fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

// Event Listeners
browseSourceButton.addActionListener(e -> selectFile(sourceField));
browseDestinationButton.addActionListener(e ->
selectFile(destinationField));
okButton.addActionListener(e -> handleCompressionOrDecompression());
clearButton.addActionListener(e -> clearFields());

setLocationRelativeTo(null);
}

private void selectFile(JTextField textField) {


int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}

private void handleCompressionOrDecompression() {


String sourcePath = sourceField.getText();
String destinationPath = destinationField.getText();
String selectedAlgorithm = (String) algorithmComboBox.getSelectedItem();
boolean isCompress = compressRadioButton.isSelected();

if (sourcePath.isEmpty() || destinationPath.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please select both source and
destination paths.");
return;
}

File sourceFile = new File(sourcePath);


File destinationFile = new File(destinationPath);

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.
}
}

private void compressZip(File sourceFile, File destinationFile) throws


IOException {
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destinationFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
ZipEntry zipEntry = new ZipEntry(sourceFile.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
outputArea.append("File compressed successfully to " +
destinationFile.getAbsolutePath() + "\n");
}
}

private void clearFields() {


sourceField.setText("");
destinationField.setText("");
outputArea.setText("");
compressRadioButton.setSelected(false);
decompressRadioButton.setSelected(false);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new FileCompressorApp().setVisible(true));
}
}

Output :
Conclusion :

The FileCompressorApp is a simple GUI tool for compressing and


decompressing files using algorithms like "Zip". It allows users to select files,
choose algorithms, and perform compression or decompression tasks, with
basic validation and error handling. It provides a foundation for adding more
algorithms and future enhancements.

You might also like