英文:
Java ZipEntry to bytes array
问题
以下是您的代码部分的中文翻译:
这是我的代码:
final InputStream inputStream = MY_RECEIVED_INPUT_STREAM;
ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
zipEntry = zis.getNextEntry();
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
zis.closeEntry();
zis.close();
我收到了一个包含许多文件的压缩文件。我想将这些文件写入数据库。我想要的是从每个ZipEntry中获取字节并将它们保存到数据库中作为 Blob(xxxxxx.... 部分)。我该如何从 zipEntry 中获取字节?
我没有 ZipFile,所以无法像这样使用:
InputStream stream = zipFile.getInputStream(entry);
或者
byte[] bytes = IOUtils.readAllBytes(zipFile.getInputStream(entry));
提前感谢您的帮助。
英文:
here is my code:
final InputStream inputStream = MY_RECEIVED_INPUT_STREAM;
ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
zipEntry = zis.getNextEntry();
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
zis.closeEntry();
zis.close();
I receive zip file with many files inside. I want to write these files to database. What I want is to get bytes from each and every ZipEntry and save them to database as Blob (xxxxxx.... part).
How can I get bytes from zipEntry?
I don't have ZipFile, so I can't use something like this:
InputStream stream = zipFile.getInputStream(entry);
or
byte[] bytes = IOUtils.readAllBytes(zipFile.getInputStream(entry));
Thanks in advance.
答案1
得分: 3
你可以像这样使用:
InputStream is = MY_RECEIVED_INPUT_STREAM;
BufferedInputStream bis = null;
ZipInputStream zis = null;
ByteArrayOutputStream out = null;
String name = null;
byte[] b = new byte[8192];
int len = 0;
try {
bis = new BufferedInputStream(is);
zis = new ZipInputStream(bis);
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
// 文件名
name = zipEntry.getName();
if (zipEntry.isDirectory()) {
// 在这个示例中,我跳过目录
continue;
}
out = new ByteArrayOutputStream();
while ((len = zis.read(b)) > 0) {
out.write(b, 0, len);
}
// 保存到数据库 - db_save(String file_name, byte[] file_bytes)
db_save(name, out.toByteArray());
out.close();
}
} finally {
if (zis != null) {
zis.close();
}
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
}
英文:
You can use something like this:
InputStream is = MY_RECEIVED_INPUT_STREAM;
BufferedInputStream bis = null;
ZipInputStream zis = null;
ByteArrayOutputStream out = null;
String name = null;
byte[] b = new byte[8192];
int len = 0;
try {
bis = new BufferedInputStream(is);
zis = new ZipInputStream(bis);
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
//name of file
name = zipEntry.getName();
if (zipEntry.isDirectory()) {
//I'm skipping directories in this example
continue;
}
out = new ByteArrayOutputStream();
while ((len = zis.read(b)) > 0) {
out.write(b, 0, len);
}
//save to DB - db_save(String file_name, byte[] file_bytes)
db_save(name,out.toByteArray());
out.close();
}
} finally {
if (zis != null) {
zis.close();
}
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
}
答案2
得分: 2
ZipFile
使得代码更易阅读,但基本规则是 ZipInputStream
被定位到与当前 ZipEntry
相关的内容。
直接从 zis
读取,直到达到 0,并且 在处理完所有条目之前不要调用 close()
。
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
// xxx 执行 BLOB 创建
zis.transferTo(outputStream); // Java9
}
(PS:不需要调用 closeEntry()
)
英文:
ZipFile
makes this easier to read but the basic rule is that the ZipInputStream
is lined up to the content relating to the current ZipEntry
.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/zip/ZipInputStream.html#read(byte%5B%5D,int,int)
Read directly from zis
until 0 and don't close()
until all entries are handled.
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
// xxx Do BLOB creation
zis.transferTo(outputStream); // Java9
}
(PS You don't need to call closeEntry()
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论