Changeset 8085 for branches/3.19-stable
- Timestamp:
- Oct 27, 2022, 7:25:30 AM (7 months ago)
- Location:
- branches/3.19-stable/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.19-stable/src/core/net/sf/basedb/util/zip/FilePacker.java
r7551 r8085 27 27 import java.io.InputStream; 28 28 import java.io.OutputStream; 29 30 import net.sf.basedb.core.PluginParameter; 31 import net.sf.basedb.core.StringParameterType; 29 32 30 33 /** … … 65 68 66 69 /** 70 Does the packer support encryption or not? 71 The default implementation return false. 72 @since 3.19.5 73 */ 74 public default boolean supportsEncryption() 75 { 76 return false; 77 } 78 79 /** 80 The default implementation return a parameter asking for a password. 81 The implementing class may create another parameter as long as it is 82 named "password". This method is only called if the implementation 83 supports encryption. 84 @since 3.19.5 85 */ 86 public default PluginParameter<String> getPasswordParameter() 87 { 88 PluginParameter<String> passwordParameter = new PluginParameter<String> 89 ( 90 "password", "Password", "Enter a password to encrypt the archive." , new StringParameterType() 91 ); 92 return passwordParameter; 93 } 94 95 /** 96 Set the password to use for encrypting the archive. This method 97 is only called if the implementation supports encryption and the 98 user has entered a password. 99 @since 3.19.5 100 */ 101 public default void setPassword(String password) 102 {} 103 104 /** 67 105 The output stream that the compressed files should be written to. 68 106 @param out The output stream to write to -
branches/3.19-stable/src/core/net/sf/basedb/util/zip/ZipFilePacker.java
r6127 r8085 23 23 package net.sf.basedb.util.zip; 24 24 25 import net.lingala.zip4j.io.outputstream.ZipOutputStream; 26 import net.lingala.zip4j.model.ZipParameters; 27 import net.lingala.zip4j.model.enums.AesKeyStrength; 28 import net.lingala.zip4j.model.enums.CompressionLevel; 29 import net.lingala.zip4j.model.enums.CompressionMethod; 30 import net.lingala.zip4j.model.enums.EncryptionMethod; 31 import net.sf.basedb.core.BaseException; 32 import net.sf.basedb.core.PluginParameter; 33 import net.sf.basedb.core.StringParameterType; 25 34 import net.sf.basedb.util.FileUtil; 26 35 … … 28 37 import java.io.InputStream; 29 38 import java.io.OutputStream; 30 import java.util.zip.ZipEntry;31 import java.util.zip.ZipOutputStream;32 39 33 40 /** … … 42 49 { 43 50 private ZipOutputStream zip; 51 private String password; 44 52 45 53 /** … … 78 86 } 79 87 /** 80 *Wrap the output stream in a {@link ZipOutputStream}.81 88 Wrap the output stream in a {@link ZipOutputStream}. 89 */ 82 90 @Override 83 91 public void setOutputStream(OutputStream out) 84 92 { 85 this.zip = new ZipOutputStream(out); 86 zip.setMethod(ZipOutputStream.DEFLATED); 93 try 94 { 95 this.zip = password == null ? new ZipOutputStream(out) : new ZipOutputStream(out, password.toCharArray()); 96 } 97 catch (IOException ex) 98 { 99 throw new BaseException(ex); 100 } 87 101 } 102 88 103 /** 89 Create a new {@link ZipEntry} and write the compressed 90 data to it. 104 Encryption is supported. 105 @since 3.19.5 106 */ 107 @Override 108 public boolean supportsEncryption() 109 { 110 return true; 111 } 112 113 /** 114 Get the encyption password parameter. 115 @since 3.19.5 116 */ 117 @Override 118 public PluginParameter<String> getPasswordParameter() 119 { 120 PluginParameter<String> passwordParameter = new PluginParameter<String> 121 ( 122 "password", "Password", "Enter a password to encrypt the archive with strong AES256 encryption. " 123 + "If empty, the ZIP archive will not be encrypted." , new StringParameterType() 124 ); 125 return passwordParameter; 126 } 127 128 /** 129 @since 3.19.5 130 */ 131 @Override 132 public void setPassword(String password) 133 { 134 this.password = password; 135 } 136 137 /** 138 Create a new entry and write the compressed data to it. 91 139 */ 92 140 @Override … … 96 144 boolean isDirectory = in == null; 97 145 if (isDirectory && !entryName.endsWith("/")) entryName += "/"; 98 ZipEntry entry = new ZipEntry(entryName); 99 if (lastModified > 0) entry.setTime(lastModified); 100 if (isDirectory) 146 147 ZipParameters zipParameters = new ZipParameters(); 148 zipParameters.setFileNameInZip(entryName); 149 if (lastModified > 0) 101 150 { 102 entry.setMethod(ZipOutputStream.STORED); 103 entry.setSize(0); 104 entry.setCrc(0); 151 zipParameters.setLastModifiedFileTime(lastModified); 152 } 153 if (isDirectory) 154 { 155 zipParameters.setCompressionMethod(CompressionMethod.STORE); 156 zipParameters.setEntrySize(0); 157 zipParameters.setEntryCRC(0); 105 158 } 106 159 else 107 160 { 108 entry.setSize(size); 161 zipParameters.setCompressionMethod(CompressionMethod.DEFLATE); 162 zipParameters.setCompressionLevel(CompressionLevel.NORMAL); 163 zipParameters.setEntrySize(size); 164 if (password != null) 165 { 166 zipParameters.setEncryptFiles(true); 167 zipParameters.setEncryptionMethod(EncryptionMethod.AES); 168 zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256); 169 } 109 170 } 110 zip.putNextEntry( entry);171 zip.putNextEntry(zipParameters); 111 172 if (!isDirectory) FileUtil.copy(in, zip); 112 173 zip.flush(); -
branches/3.19-stable/src/plugins/core/net/sf/basedb/plugins/PackedFileExporter.java
r7605 r8085 308 308 309 309 // Other options 310 if (packer.supportsEncryption()) 311 { 312 storeValue(job, request, ri.getParameter("password")); 313 } 310 314 storeValue(job, request, ri.getParameter("removeItems")); 311 315 if (saveAs != null) … … 358 362 List<Integer> directories = job.getValues("directories"); 359 363 boolean removeItems = Boolean.TRUE.equals(job.getValue("removeItems")); 364 String password = job.getValue("password"); 360 365 FilePacker packer = getPacker(); 366 if (packer.supportsEncryption() && password != null) 367 { 368 packer.setPassword(password); 369 } 361 370 362 371 ChainedProgressReporter chained = null; … … 528 537 parameters.add(getSaveAsParameter(null, null, defaultPath, requireFile)); 529 538 parameters.add(getOverwriteParameter(null, null)); 539 // Encryption 540 if (packer.supportsEncryption()) 541 { 542 parameters.add(packer.getPasswordParameter()); 543 } 530 544 531 545 // Remove parameter
Note: See TracChangeset
for help on using the changeset viewer.