Ejemplo n.º 1
0
        public static void unzipFile(string inputZip, string destinationDirectory)
        {
            int           buffer         = 2048;
            List <string> zipFiles       = new List <string>();
            File          sourceZipFile  = new File(inputZip);
            File          unzipDirectory = new File(destinationDirectory);

            createDir(unzipDirectory.AbsolutePath);

            ZipFile zipFile;

            zipFile = new ZipFile(sourceZipFile, ZipFile.OpenRead);
            IEnumeration zipFileEntries = zipFile.Entries();

            while (zipFileEntries.HasMoreElements)
            {
                ZipEntry entry        = (ZipEntry)zipFileEntries.NextElement();
                string   currentEntry = entry.Name;
                File     destFile     = new File(unzipDirectory, currentEntry);

                if (currentEntry.EndsWith(Constant.SUFFIX_ZIP))
                {
                    zipFiles.Add(destFile.AbsolutePath);
                }

                File destinationParent = destFile.ParentFile;
                createDir(destinationParent.AbsolutePath);

                if (!entry.IsDirectory)
                {
                    if (destFile != null && destFile.Exists())
                    {
                        LogUtils.i(destFile + "已存在");
                        continue;
                    }

                    BufferedInputStream inputStream = new BufferedInputStream(zipFile.GetInputStream(entry));
                    int currentByte;
                    // buffer for writing file
                    byte[] data = new byte[buffer];

                    var fos = new System.IO.FileStream(destFile.AbsolutePath, System.IO.FileMode.OpenOrCreate);
                    BufferedOutputStream dest = new BufferedOutputStream(fos, buffer);

                    while ((currentByte = inputStream.Read(data, 0, buffer)) != -1)
                    {
                        dest.Write(data, 0, currentByte);
                    }
                    dest.Flush();
                    dest.Close();
                    inputStream.Close();
                }
            }
            zipFile.Close();

            foreach (var zipName in zipFiles)
            {
                unzipFile(zipName, destinationDirectory + File.SeparatorChar
                          + zipName.Substring(0, zipName.LastIndexOf(Constant.SUFFIX_ZIP)));
            }
        }