zip4j,从输入流中提取受密码保护的文件(blob 输入流,它是一个 zip 文件)
zip4j, extract a password protected file from an inputstream (blob inputstream which is a zip file)
我有一个数据库,其中包含 blob 和该数据库中受密码保护的 zip,使用我通常看到的标准文件对象方法
File zipFile = new File("C:\file.zip");
net.lingala.zip4j.core.ZipFile table = new net.lingala.zip4j.core.ZipFile(zipFile);
if (table.isEncrypted())
table.setPassword(password);
net.lingala.zip4j.model.FileHeader entry = table.getFileHeader("file_inside_the_zip.txt");
return table.getInputStream(entry); //Decrypted inputsteam!
我的问题是,我如何在不使用临时文件的情况下实现这样的东西,并且纯粹单独获取 blob 的输入流,到目前为止我有这样的东西
InputStream zipStream = getFileFromDataBase("stuff.zip");
//This point forward I have to save zipStream as a temporary file and use the traditional code above
我认为通过 zip4j 是不可能的,因为它非常以文件为中心。
看看这个:http://blog.alutam.com/2012/03/31/new-library-for-reading-and-writing-zip-files-in-java/
有一种方法可以通过 net.lingala.zip4j.io.inputstream.ZipInputStream
实现
(给定一个 byte[] zipFile 和一个 String 密码)
String zipPassword = "abcabc";
ZipInputStream innerZip = new ZipInputStream(new ByteArrayInputStream(zipFile), zipPassword.toCharArray());
然后你可以遍历你的非保护 zip
File zip = null;
while ((zipEntry = zipIs.getNextEntry()) != null) {
zip = new File(file.getAbsolutePath(), zipEntry.getFileName());
....
}
我在 Hadoop 文件系统 (HDFS) 中处理受密码保护的压缩文件时遇到了同样的问题。 HDFS 不知道文件对象。
这就是我使用 zip4j 的方法:
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path hdfsReadPath = new Path(zipFilePath); // like "hdfs://master/dir/sub/data/the.zip"
FSDataInputStream inStream = fs.open(hdfsReadPath);
ZipInputStream zipInputStream = new ZipInputStream(inStream, passWord.toCharArray());
LocalFileHeader zipEntry = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream));
while ((zipEntry = zipInputStream.getNextEntry()) != null ) {
String entryName = zipEntry.getFileName();
System.out.println(entryName);
if (!zipEntry.isDirectory()) {
String line;
while ((line = reader.readLine()) != null) {
//process the line
}
}
}
reader.close();
zipInputStream.close();
public void extractWithZipInputStream(File zipFile, char[] password) throws IOException {
LocalFileHeader localFileHeader;
int readLen;
byte[] readBuffer = new byte[4096];
InputStream inputStream = new FileInputStream(zipFile);
try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, password)) {
while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
File extractedFile = new File(localFileHeader.getFileName());
try (OutputStream outputStream = new FileOutputStream(extractedFile)) {
while ((readLen = zipInputStream.read(readBuffer)) != -1) {
outputStream.write(readBuffer, 0, readLen);
}
}
}
}
}
此方法需要根据您的需要进行修改。例如,您可能必须更改输出位置。我已经尝试过了,它奏效了。为了更好地理解,请参阅 https://github.com/srikanth-lingala/zip4j
我有一个数据库,其中包含 blob 和该数据库中受密码保护的 zip,使用我通常看到的标准文件对象方法
File zipFile = new File("C:\file.zip");
net.lingala.zip4j.core.ZipFile table = new net.lingala.zip4j.core.ZipFile(zipFile);
if (table.isEncrypted())
table.setPassword(password);
net.lingala.zip4j.model.FileHeader entry = table.getFileHeader("file_inside_the_zip.txt");
return table.getInputStream(entry); //Decrypted inputsteam!
我的问题是,我如何在不使用临时文件的情况下实现这样的东西,并且纯粹单独获取 blob 的输入流,到目前为止我有这样的东西
InputStream zipStream = getFileFromDataBase("stuff.zip");
//This point forward I have to save zipStream as a temporary file and use the traditional code above
我认为通过 zip4j 是不可能的,因为它非常以文件为中心。
看看这个:http://blog.alutam.com/2012/03/31/new-library-for-reading-and-writing-zip-files-in-java/
有一种方法可以通过 net.lingala.zip4j.io.inputstream.ZipInputStream
实现(给定一个 byte[] zipFile 和一个 String 密码)
String zipPassword = "abcabc";
ZipInputStream innerZip = new ZipInputStream(new ByteArrayInputStream(zipFile), zipPassword.toCharArray());
然后你可以遍历你的非保护 zip
File zip = null;
while ((zipEntry = zipIs.getNextEntry()) != null) {
zip = new File(file.getAbsolutePath(), zipEntry.getFileName());
....
}
我在 Hadoop 文件系统 (HDFS) 中处理受密码保护的压缩文件时遇到了同样的问题。 HDFS 不知道文件对象。
这就是我使用 zip4j 的方法:
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path hdfsReadPath = new Path(zipFilePath); // like "hdfs://master/dir/sub/data/the.zip"
FSDataInputStream inStream = fs.open(hdfsReadPath);
ZipInputStream zipInputStream = new ZipInputStream(inStream, passWord.toCharArray());
LocalFileHeader zipEntry = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream));
while ((zipEntry = zipInputStream.getNextEntry()) != null ) {
String entryName = zipEntry.getFileName();
System.out.println(entryName);
if (!zipEntry.isDirectory()) {
String line;
while ((line = reader.readLine()) != null) {
//process the line
}
}
}
reader.close();
zipInputStream.close();
public void extractWithZipInputStream(File zipFile, char[] password) throws IOException {
LocalFileHeader localFileHeader;
int readLen;
byte[] readBuffer = new byte[4096];
InputStream inputStream = new FileInputStream(zipFile);
try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, password)) {
while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
File extractedFile = new File(localFileHeader.getFileName());
try (OutputStream outputStream = new FileOutputStream(extractedFile)) {
while ((readLen = zipInputStream.read(readBuffer)) != -1) {
outputStream.write(readBuffer, 0, readLen);
}
}
}
}
}
此方法需要根据您的需要进行修改。例如,您可能必须更改输出位置。我已经尝试过了,它奏效了。为了更好地理解,请参阅 https://github.com/srikanth-lingala/zip4j