1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public class UnzipFiler { private final static int CHUNK_SIZE = 32 * 1024; byte [] _fileIOBuffer = new byte [CHUNK_SIZE]; public void unzipFile(File zipFile, String directory) throws IOException { ZipInputStream in = null ; FileOutputStream os = null ; try { in = new ZipInputStream ( new FileInputStream(zipFile)); ZipEntry entry = null ; while ((entry = in .getNextEntry ())!= null ) { String entryName = entry.getName(); if (entry.isDirectory ()) { File file = new File (directory, entryName); file.mkdirs(); } else { File file = new File(directory, entryName); if (file.exists()){ file.delete(); } os = new FileOutputStream (file); int unzippedSize = 0; int reportedProgress = 100; int bytesRead = 0; while ((bytesRead = in .read (_fileIOBuffer))!= -1) { os.write(_fileIOBuffer, 0, bytesRead); unzippedSize += bytesRead; } os.close(); } } } catch (FileNotFoundException e) { Log.v( "unzip" , e.getMessage()); } catch (IOException e) { Log.v( "unzip" , e.getMessage()); } finally { if ( in != null ){ in .close(); } if (os != null ){ os.close(); } } } } |
Stack overflowに投稿したのと同じものだけれど、こうした方が良い、などの意見があったらコメントお願いします。
How should I decompress a large data file in AsyncTask.doInBackground?