java中受密码保护的zip文件
我已经使用java创建了zip文件,如下代码片段所示java中受密码保护的zip文件,java,zip,Java,Zip,我已经使用java创建了zip文件,如下代码片段所示 import java.io.*; import java.util.zip.*; public class ZipCreateExample { public static void main(String[] args) throws IOException { System.out.print("Please enter file name to zip : "); BufferedReader input = n
import java.io.*;
import java.util.zip.*;
public class ZipCreateExample {
public static void main(String[] args) throws IOException {
System.out.print("Please enter file name to zip : ");
BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
String filesToZip = input.readLine();
File f = new File(filesToZip);
if(!f.exists()) {
System.out.println("File not found.");
System.exit(0);
}
System.out.print("Please enter zip file name : ");
String zipFileName = input.readLine();
if (!zipFileName.endsWith(".zip"))
zipFileName = zipFileName + ".zip";
byte[] buffer = new byte[18024];
try {
ZipOutputStream out = new ZipOutputStream
(new FileOutputStream(zipFileName));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
FileInputStream in = new FileInputStream(filesToZip);
out.putNextEntry(new ZipEntry(filesToZip));
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
in.close();
out.close();
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
System.exit(0);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
System.exit(0);
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(0);
}
}
}
现在我想当我点击zip文件时,它会提示我输入密码,然后解压缩zip文件。
请提供帮助,我该如何进一步?标准Java API不支持密码保护的zip文件。幸运的是,好人已经为我们实现了这样的能力。请阅读这篇解释如何创建受密码保护的zip的文章。
(链接已失效,最新存档版本:)没有用于创建密码保护文件的默认Java API。还有另一个示例说明如何执行此操作。下面的示例代码将为您的文件提供zip和密码保护。 此REST服务接受原始文件的字节。它对字节数组进行压缩,密码对其进行保护。然后,它发送受密码保护的压缩文件字节作为响应。该代码是向REST服务发送和接收二进制字节以及使用密码保护压缩文件的示例。字节从流中压缩,因此服务器上从未存储任何文件
- 在java中使用Jersey API使用JAX-RS API
- 客户端正在使用Jersey客户端API
- 使用zip4j 1.3.2开源库和apache commons io李>
@Test
public void testPassProtectZip_with_params() {
byte[] inputBytes = null;
try {
inputBytes = FileUtils.readFileToByteArray(new File(inputFilePath));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("bytes read into array. size = " + inputBytes.length);
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080").path("filezip/services/zip/bindata/protect/qparam");
target = target.queryParam("pass", "mypass123");
target = target.queryParam("inputFileName", "any_name_here.pdf");
Invocation.Builder builder = target.request(MediaType.APPLICATION_OCTET_STREAM);
Response resp = builder.put(Entity.entity(inputBytes, MediaType.APPLICATION_OCTET_STREAM));
System.out.println("response = " + resp.getStatus());
Assert.assertEquals(Status.OK.getStatusCode(), resp.getStatus());
byte[] zipBytes = resp.readEntity(byte[].class);
try {
FileUtils.writeByteArrayToFile(new File(responseFilePathPasswordZipParam), zipBytes);
} catch (IOException e) {
e.printStackTrace();
}
}
请随意使用和修改。如果您发现任何错误,请告诉我。希望这有帮助
编辑1-使用QueryParam,但您可以将HeaderParam用于PUT,以隐藏passwd。相应地修改测试方法
编辑2-REST路径为filezip/services/zip/bindata/protect/qparam
filezip是战争的名字。服务是web.xml中的url映射。zip是类级别的路径注释。bindata/protect/qparam是方法级路径注释 请尝试以下基于的代码: 是从哪里来的 用法示例:
public static void main(String[] arguments) throws ZipException
{
Zipper zipper = new Zipper("password");
zipper.pack("encrypt-me.txt");
zipper.unpack("encrypt-me", "D:\\");
}
在新版本的Zip4j中,类Zip4jConstants被删除。使用EncryptionMethod和AesKeyStrength类代替。文件:
ZipParameters ZipParameters=新的ZipParameters();
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
List filesToAdd=Arrays.asList(
新文件(“somefile”),
新文件(“其他文件”)
);
ZipFile ZipFile=新的ZipFile(“filename.zip”,“password.tocharray());
添加文件(filesToAdd、zippParameters);
您必须更加清楚问题所在。当您尝试打开zip文件时,实际会发生什么情况?这里还提到了其他选项:不确定您是否仍然在@AlexR附近,但您的链接不再有效:(@SolarUnix it works,你试用时可能网站有点宕机了。@AlexR链接在哪里?@shihabudheenk原始网站死了,所以有人删除了链接。我添加了一个存档版本。可以在这里找到库和完整示例:它与JDK7@AmitKumarGupta:是的为什么我们需要在inpu中创建ByteArrayInputStreamtByteStream。我们可以简化它并改用它吗:outputZipStream.write(fileBytes)@RuntimeException
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.commons.io.FilenameUtils;
import java.io.File;
public class Zipper
{
private String password;
private static final String EXTENSION = "zip";
public Zipper(String password)
{
this.password = password;
}
public void pack(String filePath) throws ZipException
{
ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
zipParameters.setPassword(password);
String baseFileName = FilenameUtils.getBaseName(filePath);
String destinationZipFilePath = baseFileName + "." + EXTENSION;
ZipFile zipFile = new ZipFile(destinationZipFilePath);
zipFile.addFile(new File(filePath), zipParameters);
}
public void unpack(String sourceZipFilePath, String extractedZipFilePath) throws ZipException
{
ZipFile zipFile = new ZipFile(sourceZipFilePath + "." + EXTENSION);
if (zipFile.isEncrypted())
{
zipFile.setPassword(password);
}
zipFile.extractAll(extractedZipFilePath);
}
}
public static void main(String[] arguments) throws ZipException
{
Zipper zipper = new Zipper("password");
zipper.pack("encrypt-me.txt");
zipper.unpack("encrypt-me", "D:\\");
}
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
List<File> filesToAdd = Arrays.asList(
new File("somefile"),
new File("someotherfile")
);
ZipFile zipFile = new ZipFile("filename.zip", "password".toCharArray());
zipFile.addFiles(filesToAdd, zipParameters);