JavaでのZip圧縮について
Android上でZipOutputStreamを使ってZipを作成するコードを書いています。
実際に動かしてみると、AndroidのESファイルエクスプローラでは正しくZipが作成されているように見えるのですが、Windowsエクスプローラで開くと空っぽです。
同じフォルダをESファイルエクスプローラで圧縮したものとサイズが数百バイトほど違っており、ヘッダーも微妙に違うようなのですが(こちらはWindowsでも正常に開けました)、何が間違っているのでしょうか。。
Java
1 private static void compressZip(String sourcePath, String outputFullPath){ 2 File[] files = collectFiles(new File(sourcePath)); 3 String parentPath; 4 5 //1つ上のフォルダのパスを取得 6 int i = sourcePath.length(); 7 while(i > 0){ 8 i--; 9 if(sourcePath.charAt(i) == '/') break; 10 } 11 parentPath = sourcePath.substring(0, i); 12 13 //バッファ 14 byte[] buffer = new byte[1024]; 15 16 try { 17 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFullPath)); 18 19 i = 0; 20 while (i < files.length) { 21 if(files[i].isFile()) { 22 InputStream is = new FileInputStream(files[i]); 23 String str = files[i].getPath().substring(parentPath.length()); 24 25 zos.putNextEntry(new ZipEntry(str)); 26 27 int len; 28 while ((len = is.read(buffer)) != -1) { 29 zos.write(buffer, 0, len); 30 } 31 32 is.close(); 33 zos.closeEntry(); 34 } 35 36 i++; 37 } 38 39 zos.close(); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 } 44 45 private static File[] collectFiles(File folder){ 46 //フォルダ内のすべてのファイルを列挙 47 File[] innerFiles = folder.listFiles(); 48 File[] temp, temp2; 49 int i = 0; 50 while(i < innerFiles.length){ 51 if(innerFiles[i].isDirectory()){ 52 //ディレクトリだった場合はその下の階層を取得して配列を結合 53 temp = collectFiles(innerFiles[i]); 54 temp2 = innerFiles; 55 innerFiles = new File[innerFiles.length + temp.length]; 56 System.arraycopy(temp2, 0, innerFiles, 0, temp2.length); 57 System.arraycopy(temp, 0, innerFiles, temp2.length, temp.length); 58 59 } 60 i++; 61 } 62 63 return innerFiles; 64 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/15 19:01