A web Service expects a byte[]
coming from a zip file.
Web服务需要来自zip文件的byte []。
I have some files in a folder that I zip with Java and then I get the byte[]
from this zip file.
我在一个文件夹中有一些文件,我用Java压缩,然后从这个zip文件中获取byte []。
Is this necessary or can I create the byte[]
straight from the folder?
这是必要的还是可以直接从文件夹创建byte []?
2 个解决方案
#1
1
I think something like this would allow you to do what you want without writing as long as the files are not going to be very big.
我认为只要文件不会很大,这样的东西就可以让你在不写的情况下做你想做的事。
String[] sourceFiles = { "C:/file1", "C:/file2" };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(baos);
byte[] buffer = new byte[4096];
for (int i = 0; i < sourceFiles.length; i++)
{
FileInputStream fin = new FileInputStream(sourceFiles[i]);
zout.putNextEntry(new ZipEntry(sourceFiles[i]));
int length;
while ((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
zout.close();
byte[] bytes = baos.toByteArray();
#2
1
A folder is a collection of files. It is a container. It does not have a byte stream to get in the first place.
文件夹是文件的集合。这是一个容器。它首先没有字节流。
On the other hand, a ZIP (or any archive) is a file. The information about the different files is stored within the ZIP file itself
另一方面,ZIP(或任何存档)是一个文件。有关不同文件的信息存储在ZIP文件本身中
However, you can iterate through the folder contents, cook up a byte array and then use it (you are doing that anyways while creating the ZIP).
但是,您可以遍历文件夹内容,烹饪一个字节数组然后使用它(在创建ZIP时,您仍然这样做)。
#1
1
I think something like this would allow you to do what you want without writing as long as the files are not going to be very big.
我认为只要文件不会很大,这样的东西就可以让你在不写的情况下做你想做的事。
String[] sourceFiles = { "C:/file1", "C:/file2" };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(baos);
byte[] buffer = new byte[4096];
for (int i = 0; i < sourceFiles.length; i++)
{
FileInputStream fin = new FileInputStream(sourceFiles[i]);
zout.putNextEntry(new ZipEntry(sourceFiles[i]));
int length;
while ((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
zout.close();
byte[] bytes = baos.toByteArray();
#2
1
A folder is a collection of files. It is a container. It does not have a byte stream to get in the first place.
文件夹是文件的集合。这是一个容器。它首先没有字节流。
On the other hand, a ZIP (or any archive) is a file. The information about the different files is stored within the ZIP file itself
另一方面,ZIP(或任何存档)是一个文件。有关不同文件的信息存储在ZIP文件本身中
However, you can iterate through the folder contents, cook up a byte array and then use it (you are doing that anyways while creating the ZIP).
但是,您可以遍历文件夹内容,烹饪一个字节数组然后使用它(在创建ZIP时,您仍然这样做)。