我试图返回一个包含两个不同输出流的ZipInputStream作为javax.ws.rs.core.Response Stream.当我进行Web服务调用以检索流时,我注意到我得到一个空流回来.我之前尝试过返回GZipInputStream,并且我已经在客户端收到了预期的流.ZipInputStream是否存在阻止正确返回的问题?我正在使用javax 2.4(servlet-api)这是我的jax-rs服务的样子(我已经简化了一下):
@GET
@Produces({"application/zip", MediaType.APPLICATION_XML})
public Response getZipFiles(@PathParam("id") final Integer id){
//Get required resources here
ByteArrayOutputStream bundledStream = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(bundledStream);
out.putNextEntry(new ZipEntry("Item A"));
out.write(outputStream.toByteArray());
out.closeEntry();
out.putNextEntry(new ZipEntry("Item B"));
out.write(defectiveBillOutputStream.toByteArray());
out.closeEntry();
out.close();
bundledStream.close();
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bundledStream.toByteArray()));
return Response.ok(zis).build();
}
Run Code Online (Sandbox Code Playgroud)
这是调用服务的代码.我使用轴1.4:
HttpMethodBase getBillGroup = null;
String id = "1234";
String absoluteUrl = baseURL + BASE_SERVICE_PATH.replace("@id@",id) ;
getZip = new GetMethod(absoluteUrl);
HttpClient httpClient = new HttpClient();
try {
httpClient.executeMethod(getZip);
}
catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) Java 7应该通过解压缩包含UTF-8以外字符集的zip存档来解决一个老问题.这可以通过构造函数来实现ZipInputStream(InputStream, Charset).到现在为止还挺好.在明确设置ISO-8859-1字符集时,我可以解压缩包含文件名的zip存档,其中包含变音符号.
但问题是:当使用流迭代流时ZipInputStream.getNextEntry(),条目的名称中包含错误的特殊字符.在我的情况下,变音符号"ü"被"?"取代 性格,这显然是错误的.有人知道如何解决这个问题吗?显然ZipEntry忽略了Charset它的基础ZipInputStream.它看起来像是另一个与zip相关的JDK错误,但我也可能做错了.
...
zipStream = new ZipInputStream(
new BufferedInputStream(new FileInputStream(archiveFile), BUFFER_SIZE),
Charset.forName("ISO-8859-1")
);
while ((zipEntry = zipStream.getNextEntry()) != null) {
// wrong name here, something like "M?nchen" instead of "München"
System.out.println(zipEntry.getName());
...
}
Run Code Online (Sandbox Code Playgroud) 如何解压缩带有 unicode 文件名的 zip 文件?这是我的代码:
try {
ZipInputStream zis = new ZipInputStream(
new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
System.setProperty("file.encoding", "UTF-8");
while (ze != null) {
String fileName = new String(ze.getName().getBytes("UTF-8"));
System.out.println(fileName);
File newFile = new File(outputFolder + File.separator + fileName );
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(newFile));
OutputStreamWriter osw = new OutputStreamWriter(outStream, Charset.forName("UTF-8"));
int ch;
StringBuffer buffer1 = new StringBuffer();
while ((ch = zis.read()) > -1) {
buffer1.append((char) ch);
}
osw.write(buffer1.toString());
osw.close();
outStream.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} …Run Code Online (Sandbox Code Playgroud) 我想编写一个方法,从单个InputStream读取ZIP中的几个XML文件.
该方法将打开ZipInputStream,并在每个xml文件上获取相应的InputStream,并将其提供给我的XML解析器.这是方法的骨架:
private void readZip(InputStream is) throws IOException {
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
if (entry.getName().endsWith(".xml")) {
// READ THE STREAM
}
entry = zis.getNextEntry();
}
}
Run Code Online (Sandbox Code Playgroud)
有问题的部分是"// READ THE STREAM".我有一个工作解决方案,它包括创建一个ByteArrayInputStream,并用它提供我的解析器.但它使用缓冲区,对于大文件,我得到一个OutOfMemoryError.这是代码,如果有人仍然感兴趣:
int count;
byte buffer[] = new byte[2048];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((count = zis.read(buffer)) != -1) { out.write(buffer, 0, count); }
InputStream is = new ByteArrayInputStream(out.toByteArray());
Run Code Online (Sandbox Code Playgroud)
理想的解决方案是使用原始ZipInputStream提供解析器.它应该有效,因为如果我只用扫描仪打印条目内容就可以了:
Scanner sc = new Scanner(zis);
while (sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
Run Code Online (Sandbox Code Playgroud)
但是......我正在使用的解析器(jdom2,但我也尝试使用javax.xml.parsers.DocumentBuilderFactory)在解析数据后关闭流:/.所以我无法获得下一个条目并继续. …
Java zipEntry.getSize()返回实际文件的大小,有时返回-1(尽管文件大小大于0)。
Java API文档说:“返回条目数据的未压缩大小;如果未知,则返回-1。”
不确定在什么情况下它将返回-1,即在什么情况下将未知?
我尝试将 Zip 从 a 复制Zipinputstream到 a Zipoutputstream。
我将 Zip 存储byte[]在 Oracle 数据库中。我Zipinputstream用来解压缩 zip(后来我想编辑 Zip),然后将它放入 aZipoutputstream以获取新的byte[]并使用此数组稍后通过.zip下载文件ServletOutputStream。当我创建一个新文件时 - 没有Zipinputstream- 它可以工作。但是当我使用时,Zipinputstream我得到了错误。
这是我的代码:
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(fileFromDataBase),
Charset.forName("UTF-8"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream, Charset.forName("UTF-8"));
ZipEntry currentEntry;
byte[] buffer = new byte[8192];
while ((currentEntry = zipInputStream.getNextEntry()) != null) {
ZipEntry newEntry = new ZipEntry(currentEntry.getName());
zos.putNextEntry(newEntry);
int length;
while ((length = zipInputStream.read(buffer)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Kotlin 和 ZipInputStream 将压缩文件读入 ByteArrayOutputStream()
val f = File("/path/to/zip/myFile.zip")
val zis = ZipInputStream(FileInputStream(f))
//loop through all entries in the zipped file
var entry = zis.nextEntry
while(entry != null) {
val baos = ByteArrayOutputStream()
//read the entry into a ByteArrayOutputStream
zis.use{ it.copyTo(baos) }
val bytes = baos.toByteArray()
System.out.println(bytes[0])
zis.closeEntry() //error thrown here on first iteration
entry = zis.nextEntry
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
java.io.IOException: Stream closed
at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:67)
at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:139)
<the code above>
Run Code Online (Sandbox Code Playgroud)
我想可能zis.use在读取条目的内容后已经关闭了条目,所以我删除了zis.closeEntry(),但是在尝试获取下一个条目时它产生了相同的错误
我知道zis.use是安全的并保证输入流已关闭,但我希望它只关闭条目而不是整个流。
打印了整个字节数组后,我知道只有 zip …
我想重置ZipInputStream(即返回到起始位置),以便按顺序读取某些文件.我怎么做?我被困了......
ZipEntry entry;
ZipInputStream input = new ZipInputStream(fileStream);//item.getInputStream());
int check =0;
while(check!=2){
entry = input.getNextEntry();
if(entry.getName().toString().equals("newFile.csv")){
check =1;
InputStreamReader inputStreamReader = new InputStreamReader(input);
reader = new CSVReader(inputStreamReader);
//read files
//reset ZipInputStream if file is read.
}
reader.close();
}
if(entry.getName().toString().equals("anotherFile.csv")){
check =2;
InputStreamReader inputStreamReader = new InputStreamReader(input);
reader = new CSVReader(inputStreamReader);
//read files
//reset ZipInputStream if file is read.
}
reader.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个应用程序,它在我的资源文件夹中读取一个zip文件,其中包含一堆图像.我正在使用ZipInputStreamAPI来读取内容,然后将每个文件写入my:Environment.getExternalStorageDirectory()目录.我有一切正常,但第一次运行应用程序将图像写入存储目录是不可思议的慢.将我的图像写入光盘大约需要5分钟.我的代码看起来像这样:
ZipEntry ze = null;
ZipInputStream zin = new ZipInputStream(getAssets().open("myFile.zip"));
String location = getExternalStorageDirectory().getAbsolutePath() + "/test/images/";
//Loop through the zip file
while ((ze = zin.getNextEntry()) != null) {
File f = new File(location + ze.getName());
//Doesn't exist? Create to avoid FileNotFoundException
if(f.exists()) {
f.createNewFile();
}
FileOutputStream fout = new FileOutputStream(f);
//Read contents and then write to file
for (c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
}
fout.close();
zin.close();
Run Code Online (Sandbox Code Playgroud)
读取特定条目的内容然后写入它的过程非常慢.我认为这更多的是阅读而不是写作.我已经读过你可以使用byte[]数组缓冲来加速这个过程,但这似乎不起作用!我尝试了这个,但它只读取了部分文件... …
我有代码,ZipInputSream转换为byte [],但我不知道如何将其转换为inputstream.
private void convertStream(String encoding, ZipInputStream in) throws IOException,
UnsupportedEncodingException
{
final int BUFFER = 1;
@SuppressWarnings("unused")
int count = 0;
byte data[] = new byte[BUFFER];
while ((count = in.read(data, 0, BUFFER)) != -1)
{
// How can I convert data to InputStream here ?
}
}
Run Code Online (Sandbox Code Playgroud) zipinputstream ×10
java ×8
inputstream ×2
zip ×2
android ×1
filenames ×1
jax-rs ×1
kotlin ×1
reset ×1
rest ×1
reusability ×1
utf-8 ×1
xml-parsing ×1