2012-07-03 42 views
0

在谷歌應用程序引擎上託管的服務器上,我試圖從另一臺服務器上下載文件,zip(使用util.zip),並上傳zip文件供以後下載。下載並壓縮後無法打開圖片

我有一個文件夾(HTML和PNG文件)中的文件。下載和zip和上傳成功。我可以下載所需的zip文件,但是我無法打開png文件,儘管我可以打開原始文件。它說該程序不支持文件格式。有趣的是,zip中的html文件沒有問題。 有人知道這裏可能是什麼問題嗎?

預先感謝您。

--THE代碼 -

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

我再說一遍,沒有什麼錯在壓縮的HTML文件,但無法打開PNG圖像。我一定在某個地方做錯了事。這當然不是壓縮,因爲我有其他的png文件從本地服務器中拉出來,幷包含在zip中,它們都很好! –

回答

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(); 
      }   
    }