Java ZipEntry to bytes array

huangapple go评论63阅读模式
英文:

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中获取字节并将它们保存到数据库中作为 Blobxxxxxx.... 部分)。我该如何从 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 相关的内容。

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/zip/ZipInputStream.html#read(byte%5B%5D,int,int)

直接从 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())

huangapple
  • 本文由 发表于 2020年7月29日 16:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63149513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定