-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipOutput.java
More file actions
74 lines (60 loc) · 2.37 KB
/
ZipOutput.java
File metadata and controls
74 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.seven.bii;
import java.io.File;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.EncryptionMethod;
/**
*
* @author 7 Bii
*/
public class ZipOutput {
//encrypts user folder
public static void Encrypt(String userFolder, String userPassword) throws ZipException
{
//Parametes to zip to
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(true);
zipParameters.setCompressionLevel(CompressionLevel.MAXIMUM);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
//tries zipping
try{
ZipFile zipFile = new ZipFile("/home/ntu-user/Users/"+userFolder+".zip", userPassword.toCharArray());
zipFile.addFolder(new File("/home/ntu-user/Users/" + userFolder), zipParameters);
File index = new File("/home/ntu-user/Users/" + userFolder);
if (index.exists()) { //checks if folder exists
index.delete();
}
//deletes weird guest zip
File myObj = new File("/home/ntu-user/Users/guest.zip");
myObj.delete();
}catch(Exception e)
{
e.printStackTrace();
}
}
//decrypts user folder
public static void Decrypt(String userFolder, String userPassword) throws ZipException{
try{
ZipFile zipFile = new ZipFile("/home/ntu-user/Users/"+userFolder+".zip", userPassword.toCharArray());
zipFile.extractAll("/home/ntu-user/Users/");
//deletes weird user subfolder
File myObj = new File("/home/ntu-user/Users/" + userFolder + ".zip");
myObj.delete();
File index = new File("/home/ntu-user/Users");
if (index.exists()) {
index.delete();
}
}
catch(ZipException e)
{
System.out.println("Failed to decrypt");
e.printStackTrace();
}
}
}