1
+ package installer ;
2
+
3
+ import javax .swing .*;
4
+ import java .awt .*;
5
+ import java .awt .event .ActionEvent ;
6
+ import java .awt .event .ActionListener ;
7
+ import java .io .*;
8
+ import java .net .HttpURLConnection ;
9
+ import java .net .URL ;
10
+ import java .util .zip .ZipEntry ;
11
+ import java .util .zip .ZipInputStream ;
12
+
13
+ public class Installer {
14
+ public static void main (String [] args ) {
15
+ // Create a new frame
16
+ JFrame frmFileDownloaderAnd = new JFrame ("File Downloader and Extractor" );
17
+ frmFileDownloaderAnd .setResizable (false );
18
+ frmFileDownloaderAnd .setTitle ("File Downloader and Extractor for v1.7" );
19
+ frmFileDownloaderAnd .setIconImage (Toolkit .getDefaultToolkit ().getImage (Installer .class .getResource ("/installer/icon/downloadicon.png" )));
20
+
21
+ // Set the size of the frame
22
+ frmFileDownloaderAnd .setSize (470 , 207 );
23
+ frmFileDownloaderAnd .getContentPane ().setLayout (new BorderLayout ());
24
+
25
+ // Specify what happens when the frame is closed
26
+ frmFileDownloaderAnd .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
27
+
28
+ // Create buttons
29
+ JButton downloadButton = new JButton ("Download File and Extract File" );
30
+
31
+ // Panel for buttons
32
+ JPanel buttonPanel = new JPanel ();
33
+ buttonPanel .add (downloadButton );
34
+
35
+ // Add button panel and progress bars to the frame
36
+ frmFileDownloaderAnd .getContentPane ().add (buttonPanel , BorderLayout .NORTH );
37
+
38
+ JButton btnStartTheApplicaton = new JButton ("Start The Applicaton" );
39
+ btnStartTheApplicaton .addActionListener (new ActionListener () {
40
+ public void actionPerformed (ActionEvent e ) { try {
41
+ // Specify the directory and JAR file name
42
+ String jarFilePath = "extracted_files-from-v1.7.JavaGameStuff/JavaGameStuff-1.7/Launcher.jar" ;
43
+ // Change this to your specific folder and JAR file
44
+ // Open the JAR file
45
+ File jarFile = new File (jarFilePath );
46
+ if (jarFile .exists ()) {
47
+ Runtime .getRuntime ().exec ("java -jar " + jarFilePath );
48
+ }
49
+ else {
50
+ System .out .println ("JAR file not found in the specified directory." );
51
+ } }
52
+ catch (IOException ex ) { ex .printStackTrace (); } }
53
+
54
+ });
55
+ buttonPanel .add (btnStartTheApplicaton );
56
+
57
+ JPanel ProgressPannel = new JPanel ();
58
+ frmFileDownloaderAnd .getContentPane ().add (ProgressPannel , BorderLayout .SOUTH );
59
+
60
+ // Create progress bars
61
+ JProgressBar downloadProgressBar = new JProgressBar (0 , 100 );
62
+ ProgressPannel .add (downloadProgressBar );
63
+ JProgressBar extractProgressBar = new JProgressBar (0 , 100 );
64
+ ProgressPannel .add (extractProgressBar );
65
+
66
+ JScrollPane scrollPane = new JScrollPane ();
67
+ frmFileDownloaderAnd .getContentPane ().add (scrollPane , BorderLayout .CENTER );
68
+
69
+ JTextPane txtpnInstuctions = new JTextPane ();
70
+ txtpnInstuctions .setFont (new Font ("Open Sans Semibold" , Font .BOLD , 11 ));
71
+ txtpnInstuctions .setEditable (false );
72
+ scrollPane .setViewportView (txtpnInstuctions );
73
+ txtpnInstuctions .setText ("Instuctions:\r \n 1. Click on Download File and Extract File\r \n 3. Click on Start the Application\r \n If You have alredy clicked on Download File and Extract File then just Click on Start The Application\r \n \r \n (Help:\r \n \r \n If the Buttons don't work (eg: Pong) then go into the folder called extracted_files-from-v1.7.JavaGameStuff then into the folder called JavaGameStuff-1.7 and then open the Launcher.jar and the buttons should work)" );
74
+
75
+ // Action listener for download button
76
+ downloadButton .addActionListener (new ActionListener () {
77
+ @ Override
78
+ public void actionPerformed (ActionEvent e ) {
79
+ SwingWorker <Void , Integer > worker = new SwingWorker <Void , Integer >() {
80
+ @ Override
81
+ protected Void doInBackground () throws Exception {
82
+ String fileUrl = "https://github.com/CodePearly/JavaGameStuff/archive/refs/tags/v1.7.zip" ; // Change this URL
83
+ String savePath = "v1.7.JavaGameStuff" ;
84
+
85
+ HttpURLConnection httpConn = (HttpURLConnection ) new URL (fileUrl ).openConnection ();
86
+ int responseCode = httpConn .getResponseCode ();
87
+
88
+ if (responseCode == HttpURLConnection .HTTP_OK ) {
89
+ int contentLength = httpConn .getContentLength ();
90
+
91
+ try (InputStream in = new BufferedInputStream (httpConn .getInputStream ());
92
+ FileOutputStream out = new FileOutputStream (savePath )) {
93
+
94
+ byte [] buffer = new byte [1024 ];
95
+ int bytesRead = -1 ;
96
+ long totalBytesRead = 0 ;
97
+
98
+ while ((bytesRead = in .read (buffer )) != -1 ) {
99
+ out .write (buffer , 0 , bytesRead );
100
+ totalBytesRead += bytesRead ;
101
+
102
+ int progress = (int ) (totalBytesRead * 100 / contentLength );
103
+ publish (progress );
104
+ }
105
+
106
+ JOptionPane .showMessageDialog (frmFileDownloaderAnd , "File Downloaded Successfully!" );
107
+ String zipFilePath = "v1.7.JavaGameStuff" ; // Change this path
108
+ String destDir = "extracted_files-from-v1.7.JavaGameStuff" ; // Change this directory
109
+
110
+ File destDirFile = new File (destDir );
111
+ if (!destDirFile .exists ()) {
112
+ destDirFile .mkdir ();
113
+ }
114
+
115
+
116
+ try (ZipInputStream zipIn = new ZipInputStream (new FileInputStream (zipFilePath ))) {
117
+ ZipEntry entry ;
118
+ long totalEntriesSize = 0 ;
119
+ long entriesProcessedSize = 0 ;
120
+
121
+ // Calculate total size of entries
122
+ while ((entry = zipIn .getNextEntry ()) != null ) {
123
+ totalEntriesSize += entry .getSize ();
124
+ }
125
+ zipIn .close ();
126
+
127
+ // Extract entries
128
+ try (ZipInputStream zipInAgain = new ZipInputStream (new FileInputStream (zipFilePath ))) {
129
+ while ((entry = zipInAgain .getNextEntry ()) != null ) {
130
+ String filePath = destDir + File .separator + entry .getName ();
131
+
132
+ if (!entry .isDirectory ()) {
133
+ try (BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (filePath ))) {
134
+ byte [] buffer2 = new byte [1024 ];
135
+ int bytesRead2 ;
136
+ while ((bytesRead2 = zipInAgain .read (buffer2 )) != -1 ) {
137
+ bos .write (buffer2 , 0 , bytesRead2 );
138
+ entriesProcessedSize += bytesRead2 ;
139
+
140
+ int progress = (int ) (entriesProcessedSize * 100 / totalEntriesSize );
141
+ publish (progress );
142
+ }
143
+ }
144
+ } else {
145
+ new File (filePath ).mkdir ();
146
+ }
147
+
148
+ zipInAgain .closeEntry ();
149
+ }
150
+
151
+ JOptionPane .showMessageDialog (frmFileDownloaderAnd , "File Extracted Successfully!" );
152
+
153
+
154
+ }
155
+ }
156
+ }
157
+ }
158
+ httpConn .disconnect ();
159
+ return null ;
160
+ }
161
+
162
+ @ Override
163
+ protected void process (java .util .List <Integer > chunks ) {
164
+ for (int value : chunks ) {
165
+ downloadProgressBar .setValue (value );
166
+ }
167
+ }
168
+
169
+ @ Override
170
+ protected void done () {
171
+ downloadButton .setEnabled (true );
172
+ }
173
+ };
174
+
175
+ worker .execute ();
176
+ downloadButton .setEnabled (false );
177
+ }
178
+ });
179
+
180
+
181
+ // Make the frame visible
182
+ frmFileDownloaderAnd .setVisible (true );
183
+ }
184
+ }
0 commit comments