|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.awt.event.ActionListener; |
| 5 | +import java.io.*; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.zip.ZipEntry; |
| 9 | +import java.util.zip.ZipInputStream; |
| 10 | + |
| 11 | +public class Main { |
| 12 | + public static void main(String[] args) { |
| 13 | + // Create a new frame |
| 14 | + JFrame frame = new JFrame("File Downloader and Extractor"); |
| 15 | + |
| 16 | + // Set the size of the frame |
| 17 | + frame.setSize(400, 300); |
| 18 | + frame.setLayout(new BorderLayout()); |
| 19 | + |
| 20 | + // Specify what happens when the frame is closed |
| 21 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 22 | + |
| 23 | + // Create buttons |
| 24 | + JButton downloadButton = new JButton("Download File"); |
| 25 | + JButton extractButton = new JButton("Extract ZIP"); |
| 26 | + |
| 27 | + // Create progress bars |
| 28 | + JProgressBar downloadProgressBar = new JProgressBar(0, 100); |
| 29 | + JProgressBar extractProgressBar = new JProgressBar(0, 100); |
| 30 | + |
| 31 | + // Panel for buttons |
| 32 | + JPanel buttonPanel = new JPanel(); |
| 33 | + buttonPanel.add(downloadButton); |
| 34 | + buttonPanel.add(extractButton); |
| 35 | + |
| 36 | + // Add button panel and progress bars to the frame |
| 37 | + frame.add(buttonPanel, BorderLayout.NORTH); |
| 38 | + frame.add(downloadProgressBar, BorderLayout.CENTER); |
| 39 | + frame.add(extractProgressBar, BorderLayout.SOUTH); |
| 40 | + |
| 41 | + // Action listener for download button |
| 42 | + downloadButton.addActionListener(new ActionListener() { |
| 43 | + @Override |
| 44 | + public void actionPerformed(ActionEvent e) { |
| 45 | + SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() { |
| 46 | + @Override |
| 47 | + protected Void doInBackground() throws Exception { |
| 48 | + String fileUrl = "https://example.com/file.zip"; // Change this URL |
| 49 | + String savePath = "downloaded_file.zip"; |
| 50 | + |
| 51 | + HttpURLConnection httpConn = (HttpURLConnection) new URL(fileUrl).openConnection(); |
| 52 | + int responseCode = httpConn.getResponseCode(); |
| 53 | + |
| 54 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 55 | + int contentLength = httpConn.getContentLength(); |
| 56 | + |
| 57 | + try (InputStream in = new BufferedInputStream(httpConn.getInputStream()); |
| 58 | + FileOutputStream out = new FileOutputStream(savePath)) { |
| 59 | + |
| 60 | + byte[] buffer = new byte[1024]; |
| 61 | + int bytesRead = -1; |
| 62 | + long totalBytesRead = 0; |
| 63 | + |
| 64 | + while ((bytesRead = in.read(buffer)) != -1) { |
| 65 | + out.write(buffer, 0, bytesRead); |
| 66 | + totalBytesRead += bytesRead; |
| 67 | + |
| 68 | + int progress = (int) (totalBytesRead * 100 / contentLength); |
| 69 | + publish(progress); |
| 70 | + } |
| 71 | + |
| 72 | + JOptionPane.showMessageDialog(frame, "File Downloaded Successfully!"); |
| 73 | + |
| 74 | + } catch (Exception ex) { |
| 75 | + ex.printStackTrace(); |
| 76 | + JOptionPane.showMessageDialog(frame, "Error: " + ex.getMessage()); |
| 77 | + } |
| 78 | + } else { |
| 79 | + JOptionPane.showMessageDialog(frame, "No file to download. Server replied with status: " + responseCode); |
| 80 | + } |
| 81 | + httpConn.disconnect(); |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void process(java.util.List<Integer> chunks) { |
| 87 | + for (int value : chunks) { |
| 88 | + downloadProgressBar.setValue(value); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void done() { |
| 94 | + downloadButton.setEnabled(true); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + worker.execute(); |
| 99 | + downloadButton.setEnabled(false); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // Action listener for extract button |
| 104 | + extractButton.addActionListener(new ActionListener() { |
| 105 | + @Override |
| 106 | + public void actionPerformed(ActionEvent e) { |
| 107 | + SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() { |
| 108 | + @Override |
| 109 | + protected Void doInBackground() throws Exception { |
| 110 | + String zipFilePath = "downloaded_file.zip"; // Change this path |
| 111 | + String destDir = "extracted_files"; // Change this directory |
| 112 | + |
| 113 | + File destDirFile = new File(destDir); |
| 114 | + if (!destDirFile.exists()) { |
| 115 | + destDirFile.mkdir(); |
| 116 | + } |
| 117 | + |
| 118 | + try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) { |
| 119 | + ZipEntry entry; |
| 120 | + long totalEntriesSize = 0; |
| 121 | + long entriesProcessedSize = 0; |
| 122 | + |
| 123 | + // Calculate total size of entries |
| 124 | + while ((entry = zipIn.getNextEntry()) != null) { |
| 125 | + totalEntriesSize += entry.getSize(); |
| 126 | + } |
| 127 | + zipIn.close(); |
| 128 | + |
| 129 | + // Extract entries |
| 130 | + try (ZipInputStream zipInAgain = new ZipInputStream(new FileInputStream(zipFilePath))) { |
| 131 | + while ((entry = zipInAgain.getNextEntry()) != null) { |
| 132 | + String filePath = destDir + File.separator + entry.getName(); |
| 133 | + |
| 134 | + if (!entry.isDirectory()) { |
| 135 | + try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) { |
| 136 | + byte[] buffer = new byte[1024]; |
| 137 | + int bytesRead; |
| 138 | + while ((bytesRead = zipInAgain.read(buffer)) != -1) { |
| 139 | + bos.write(buffer, 0, bytesRead); |
| 140 | + entriesProcessedSize += bytesRead; |
| 141 | + |
| 142 | + int progress = (int) (entriesProcessedSize * 100 / totalEntriesSize); |
| 143 | + publish(progress); |
| 144 | + } |
| 145 | + } |
| 146 | + } else { |
| 147 | + new File(filePath).mkdir(); |
| 148 | + } |
| 149 | + |
| 150 | + zipInAgain.closeEntry(); |
| 151 | + } |
| 152 | + |
| 153 | + JOptionPane.showMessageDialog(frame, "File Extracted Successfully!"); |
| 154 | + |
| 155 | + } catch (Exception ex) { |
| 156 | + ex.printStackTrace(); |
| 157 | + JOptionPane.showMessageDialog(frame, "Error: " + ex.getMessage()); |
| 158 | + } |
| 159 | + } catch (Exception ex) { |
| 160 | + ex.printStackTrace(); |
| 161 | + JOptionPane.showMessageDialog(frame, "Error: " + ex.getMessage()); |
| 162 | + } |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + protected void process(java.util.List<Integer> chunks) { |
| 168 | + for (int value : chunks) { |
| 169 | + extractProgressBar.setValue(value); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + protected void done() { |
| 175 | + extractButton.setEnabled(true); |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + worker.execute(); |
| 180 | + extractButton.setEnabled(false); |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + // Make the frame visible |
| 185 | + frame.setVisible(true); |
| 186 | + } |
| 187 | +} |
0 commit comments