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?
0 件のコメント:
コメントを投稿