0

在托管在谷歌应用引擎上的服务器上,我正在尝试从另一台服务器下载文件,压缩(使用 util.zip)它们并上传 zip 文件以供以后下载。

我在文件夹中有文件(html 和 png 文件)。下载并压缩并上传成功。我可以下载所需的 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());
}
4

1 回答 1

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();
            }           
    }
于 2012-07-03T17:05:13.670 回答