在谷歌应用引擎上托管的服务器上,我试图从其他服务器下载文件,压缩(使用util.zip),然后上传zip文件以便以后下载。
我在文件夹中有文件(html和png文件)。下载和zip和上传成功。我可以下载所需的zip,但是我无法打开png文件,即使我可以使用原始文件。它说该程序不支持文件格式。有趣的是,zip中的html文件没有问题。 有人知道这里有什么问题吗?
提前谢谢。
- 代码 -
public boolean generateZip(){
byte[] application = new byte[1500000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(baos);
//This will get the desired file names and locations from the other server
ArrayList<String> others = getFileNames(this);
for(String s: others){
URL url = new URL("http://otherserver.com/" + s);
BufferedReader reader = null;
try{
//I need only the file names not the full directory name
int toSub = s.lastIndexOf("/");
String entryString = s.substring(toSub+1);
out.putNextEntry(new ZipEntry(entryString));
reader = new BufferedReader(new InputStreamReader(url.openStream()));
byte[] buffer = new byte[3000000];
int bindex = 0;
int b = reader.read();
while(b != -1){
buffer[bindex] = (byte) b;
bindex++;
b = reader.read();
}
out.write(buffer,0,bindex);
out.closeEntry();
reader.close();
System.out.println(entryString + " packaged...");
}catch(Exception e){
e.printStackTrace();
}
}
}
out.close();
} catch (IOException e) {
System.out.println("There was an error generating ZIP.");
e.printStackTrace();
}
return uploadZip(baos.toByteArray());
}
答案 0 :(得分:0)
最后我找到了解决方案。对于任何有兴趣的人:
for(String s: others){
URL url = new URL("http://otherserver.com" + s);
System.out.println("Reading " + s);
try{
int toSub = s.lastIndexOf("/");
String entryString = s.substring(toSub+1);
out.putNextEntry(new ZipEntry(entryString));
BufferedInputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
BufferedOutputStream out2 = new BufferedOutputStream(baos2);
int i;
while ((i = in.read()) != -1) {
out2.write(i);
}
out2.flush();
byte[] data = baos2.toByteArray();
// closing all the shits
out2.close();
in.close();
out.write(data,0,data.length);
out.closeEntry();
System.out.println(entryString + " packaged...");
}catch(Exception e){
e.printStackTrace();
}
}