0

Google App Engine でホストされているサーバーで、別のサーバーからファイルをダウンロードし、(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());
}
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 に答える