1
- /*
2
- * |
3
- * ||
4
- * |||||| ||||||||| |||||||| ||||||||| ||||||| ||| ||| ||||||| ||||||||| |||||| ||||||||
5
- * ||| || ||| || || ||| ||| || || ||| |||
6
- * ||| |||||||| ||| |||||||| |||||| |||||||| |||||| |||||||| ||| |||
7
- * ||| ||| ||| ||| ||| ||| ||| ||| ||| || || ||| ||| ||| |||
8
- * |||||| ||| ||| ||| ||| ||| ||| ||| ||| || || ||| ||| |||||| |||
9
- * ||
10
- * |
11
- *
12
- * A Cross Platform OS Shell
13
- * Powered By Truncheon Core
14
- */
15
-
16
1
package Cataphract .API .Wraith .Archive ;
17
2
18
- import java .io .FileInputStream ;
3
+ import java .io .FileInputStream ;
19
4
import java .io .FileNotFoundException ;
20
5
import java .io .FileOutputStream ;
21
-
22
6
import java .nio .file .Files ;
23
7
import java .nio .file .Path ;
24
8
import java .nio .file .Paths ;
25
-
26
9
import java .util .zip .ZipEntry ;
27
10
import java .util .zip .ZipInputStream ;
28
11
32
15
import Cataphract .API .Wraith .FileWrite ;
33
16
34
17
/**
35
- * A utility class for unzipping files.
36
- *
37
- * @author DAK404 (https://github.com/DAK404)
38
- * @version 1.0.0 (11-October-2023, Cataphract)
39
- * @since 0.0.1 (Cataphract 0.0.1)
40
- */
18
+ * A utility class for unzipping files.
19
+ *
20
+ * @author DAK404 (https://github.com/DAK404)
21
+ * @version 1.0.0 (11-October-2023, Cataphract)
22
+ * @since 0.0.1 (Cataphract 0.0.1)
23
+ */
41
24
public class FileUnzip
42
25
{
43
26
/** Store value of user privileges. */
44
- private boolean isUserAdmin = false ;
27
+ private boolean _isUserAdmin = false ;
45
28
46
- /** Store value of the update mode; True if program is updating, False if program is not*/
29
+ /** Store value of the update mode; True if program is updating, False if program is not */
47
30
private boolean updateMode = false ;
48
31
49
32
/**
50
- * Constructor to check if the current user is an administrator or not.
51
- *
52
- * @param username The currently logged in username.
53
- * @throws Exception Throws any exceptions encountered during runtime.
54
- */
55
- public FileUnzip (String username )throws Exception
33
+ * Constructor to check if the current user is an administrator or not.
34
+ *
35
+ * @param username The currently logged in username.
36
+ * @throws Exception Throws any exceptions encountered during runtime.
37
+ */
38
+ public FileUnzip (String username ) throws Exception
56
39
{
57
- isUserAdmin = new Login (username ).checkPrivilegeLogic ();
40
+ _isUserAdmin = new Login (username ).checkPrivilegeLogic ();
58
41
}
59
42
60
43
/**
61
- * Unzips a user file in the specified destination directory
62
- *
63
- * @param fileName Name of the file to be unzipped.
64
- * @param unzipDestination Destination for the file to be unzipped in.
65
- * @throws Exception Throws any exceptions encountered during runtime.
66
- */
67
- public void unzip (String fileName , String unzipDestination )throws Exception
44
+ * Unzips a user file in the specified destination directory
45
+ *
46
+ * @param fileName Name of the file to be unzipped.
47
+ * @param unzipDestination Destination for the file to be unzipped in.
48
+ * @throws Exception Throws any exceptions encountered during runtime.
49
+ */
50
+ public void unzip (String fileName , String unzipDestination ) throws Exception
68
51
{
52
+ fileName = IOStreams .convertFileSeparator (fileName );
53
+ unzipDestination = IOStreams .convertFileSeparator (unzipDestination );
69
54
// Check the policy if file unzip is allowed in the policy file, can be bypassed by the accounts with administrator privileges
70
- if (new PolicyCheck ().retrievePolicyValue ("filemgmt" ).equals ("on" ) || isUserAdmin )
71
- unzipLogic (fileName , unzipDestination );
55
+ if (new PolicyCheck ().retrievePolicyValue ("filemgmt" ).equals ("on" ) || _isUserAdmin )
56
+ unzipLogic (fileName , unzipDestination );
72
57
else
73
- IOStreams .printError ("Policy Management System - Permission Denied." );
58
+ IOStreams .printError ("Policy Management System - Permission Denied." );
74
59
}
75
60
76
61
/**
77
- * Installs an update by unzipping the "Update.zip" file.
78
- *
79
- * @throws Exception Throws any exceptions encountered during runtime.
80
- */
81
- public void installUpdate ()throws Exception
62
+ * Installs an update by unzipping the "Update.zip" file.
63
+ *
64
+ * @throws Exception Throws any exceptions encountered during runtime.
65
+ */
66
+ public void installUpdate () throws Exception
82
67
{
83
68
updateMode = true ;
84
- if (new PolicyCheck ().retrievePolicyValue ("update" ).equals ("on" ) || isUserAdmin )
85
- unzipLogic ("./ Update.zip", "./" );
69
+ if (new PolicyCheck ().retrievePolicyValue ("update" ).equals ("on" ) || _isUserAdmin )
70
+ unzipLogic (IOStreams . convertFileSeparator ( ".| Update.zip"), IOStreams . convertFileSeparator ( ".|" ) );
86
71
else
87
- IOStreams .printError ("Policy Management System - Permission Denied." );
72
+ IOStreams .printError ("Policy Management System - Permission Denied." );
73
+ updateMode = false ;
88
74
}
89
75
90
76
/**
91
- * Unzips a given zip file into the specified output directory.
92
- *
93
- * @param zipFilePath Path to the zip file.
94
- * @param outputDirectory Directory where the unzipped files will be placed.
95
- */
77
+ * Unzips a given zip file into the specified output directory.
78
+ *
79
+ * @param zipFilePath Path to the zip file.
80
+ * @param outputDirectory Directory where the unzipped files will be placed.
81
+ */
96
82
private void unzipLogic (String zipFilePath , String outputDirectory )
97
83
{
98
84
try
@@ -121,8 +107,8 @@ private void unzipLogic(String zipFilePath, String outputDirectory)
121
107
// Create directories for file entries and write the file content
122
108
Files .createDirectories (entryPath .getParent ());
123
109
try
124
- {
125
- if (updateMode )
110
+ {
111
+ if (updateMode )
126
112
{
127
113
IOStreams .printInfo ("Installing File: " + entryName );
128
114
FileWrite .logger ("Installing: " + entryName , "Update" );
@@ -132,18 +118,20 @@ private void unzipLogic(String zipFilePath, String outputDirectory)
132
118
int bytesRead ;
133
119
134
120
while ((bytesRead = zipIn .read (buffer )) != -1 )
135
- fos .write (buffer , 0 , bytesRead );
121
+ {
122
+ fos .write (buffer , 0 , bytesRead );
123
+ }
124
+ fos .close (); // Close the FileOutputStream here
136
125
}
137
- catch (Exception e )
126
+ catch (Exception e )
138
127
{
139
128
IOStreams .printError ("File Error: " + entryName );
140
129
}
141
130
}
142
131
}
143
132
zipIn .closeEntry ();
144
- fos .close ();
145
- }
146
- catch (Exception e )
133
+ }
134
+ catch (Exception e )
147
135
{
148
136
IOStreams .println ("Error: " + e );
149
137
e .printStackTrace ();
0 commit comments