示例#1
0
 /// <summary>
 /// 解压文件,重名则覆盖
 /// </summary>
 /// <param name="filePath">压缩文件路径</param>
 /// <param name="targetPath">解压目标目录</param>
 /// <returns></returns>
 public static async Task UnCompress(string filePath, string targetPath)
 {
     CreateDirectories(targetPath);
     using (ZipInputStream stream = new ZipInputStream(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
     {
         ZipEntry entry;
         while ((entry = stream.GetNextEntry()) != null)
         {
             if (entry.IsDirectory)
             {
                 string directoryPath = Path.Combine(targetPath, Path.GetDirectoryName(entry.Name));
                 CreateDirectories(directoryPath);
             }
             else if (entry.IsFile)
             {
                 var f = Path.Combine(targetPath, entry.Name);
                 CreateDirectories(Path.GetDirectoryName(f));
                 using (FileStream streamWriter = File.Create(f))
                 {
                     int    size = 0;
                     byte[] data = new byte[1024];
                     while ((size = await stream.ReadAsync(data, 0, data.Length)) > 0)
                     {
                         await streamWriter.WriteAsync(data, 0, size);
                     }
                 }
             }
         }
     }
 }
示例#2
0
        async Task UnZip(Stream zipStream, string extractDirectory, string password = "")
        {
            using (ZipInputStream zipInputStream = new ZipInputStream(zipStream))
            {
                // Set a zip password if it's required
                if (password.Length > 0)
                {
                    zipInputStream.Password = password;
                }
                ZipEntry zipEntry = null;
                while ((zipEntry = zipInputStream.GetNextEntry()) != null)
                {
                    Debug.Log("Unzipping: " + zipStream.Position + "/" + zipStream.Length);
                    onUnzippingProgress.Invoke(zipStream.Position, zipStream.Length);
                    Debug.Log("Unzipping Entry: " + zipEntry.Name);
                    onUnzippingFileName.Invoke(zipEntry.Name);
                    if (!zipEntry.IsFile)
                    {
                        string directoryName = Path.Combine(extractDirectory, Path.GetDirectoryName(zipEntry.Name));
                        // Create directory
                        if (directoryName.Length > 0 && !Directory.Exists(directoryName))
                        {
                            Directory.CreateDirectory(directoryName);
                        }
                    }
                    else
                    {
                        string extractPath = Path.Combine(extractDirectory, zipEntry.Name);
                        int    totalCount  = 0;
                        int    count;
                        int    bufferSize = 1024 * 1000;
                        byte[] buff       = new byte[bufferSize];
                        using (FileStream writeFileStream = File.Create(extractPath))
                        {
                            while ((count = await zipInputStream.ReadAsync(buff, 0, bufferSize)) > 0)
                            {
                                totalCount += count;
                                await writeFileStream.WriteAsync(buff, 0, count);

                                writeFileStream.Flush();
                                Debug.Log("Unzipping Entry: " + totalCount + "/" + zipInputStream.Length);
                                onUnzippingFileProgress.Invoke(totalCount, zipInputStream.Length);
                                if (destroyed)
                                {
                                    break;
                                }
                            }
                            writeFileStream.Flush();
                            writeFileStream.Close();
                        }
                    }
                    if (destroyed)
                    {
                        break;
                    }
                }
                zipInputStream.Close();
            }
        }
示例#3
0
        private async Task <bool> UnzipFileAsync(string zipFilePath, string unzipFolderPath)
        {
            try
            {
                var entry        = new ZipEntry(Path.GetFileNameWithoutExtension(zipFilePath));
                var fileStreamIn = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read);
                var zipInStream  = new ZipInputStream(fileStreamIn);
                entry = zipInStream.GetNextEntry();
                while (entry != null && entry.CanDecompress)
                {
                    var outputFile      = unzipFolderPath + @"/" + entry.Name;
                    var outputDirectory = Path.GetDirectoryName(outputFile);
                    if (!Directory.Exists(outputDirectory))
                    {
                        Directory.CreateDirectory(outputDirectory);
                    }

                    if (entry.IsFile)
                    {
                        var    fileStreamOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
                        int    size;
                        byte[] buffer = new byte[4096];
                        do
                        {
                            size = await zipInStream.ReadAsync(buffer, 0, buffer.Length);

                            await fileStreamOut.WriteAsync(buffer, 0, size);
                        } while (size > 0);
                        fileStreamOut.Close();
                    }

                    entry = zipInStream.GetNextEntry();
                }
                zipInStream.Close();
                fileStreamIn.Close();
            }
            catch
            {
                return(false);
            }
            return(true);
        }
示例#4
0
        public static async Task <MemoryStream> ReadZipFileToMemoryAsync(ZipInputStream zip)
        {
            MemoryStream ms = new MemoryStream();

            byte[] buffer = new byte[1048576];
            while (true)
            {
                int i = await zip.ReadAsync(buffer, 0, 1048576);

                if (i > 0)
                {
                    ms.Write(buffer, 0, 1048576);
                }
                else
                {
                    break;
                }
            }
            return(ms);
        }
示例#5
0
        /// <summary>
        /// <inheritdoc cref="Decompress(string, string)"/>
        /// </summary>
        /// <param name="zipFile">zip文件路径</param>
        /// <param name="unzipPath">要解压到的路径</param>
        /// <param name="passCode">压缩文件密码</param>
        public static async void Decompress(string zipFile, string unzipPath, string passCode)
        {
            Debug.Print(string.Format("开始解压压缩文件{0}", zipFile));
            zipFile = VerifyFileName(zipFile);
            if (!File.Exists(zipFile) || !VerifyArchive(zipFile))
            {
                return;
            }
            if (!Directory.Exists(unzipPath))
            {
                Directory.CreateDirectory(unzipPath);
            }
            using (ZipInputStream zipStream = new ZipInputStream(new FileStream(zipFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                zipStream.Password = passCode;
                ZipEntry zipEntry = null;
                try
                {
                    while ((zipEntry = zipStream.GetNextEntry()) != null)
                    {
                        Debug.Print(string.Format("正在解压{0}", zipEntry.Name));
                        if (string.IsNullOrEmpty(zipEntry.Name))
                        {
                            continue;
                        }
                        string fullUnzipPath = Path.Combine(unzipPath, zipEntry.Name);
                        if (zipEntry.IsDirectory)
                        {
                            FileAttributes fileAttributes = FileAttributes.Normal;
                            Directory.CreateDirectory(fullUnzipPath);
                            if (zipEntry.Name.StartsWith('.'))
                            {
                                File.SetAttributes(fullUnzipPath, fileAttributes | FileAttributes.Hidden);
                            }
                            OnDecompress?.Invoke(zipEntry.Name, 1, 1, true);
                        }
                        else if (zipEntry.IsFile)
                        {
                            using (FileStream writeStream = new FileStream(fullUnzipPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                            {
                                int    readNum   = 0;
                                uint   writeNum  = 0;
                                byte[] dataBytes = new byte[10240];
                                do
                                {
                                    readNum = await zipStream.ReadAsync(dataBytes, 0, dataBytes.Length);

                                    writeNum += (uint)readNum;
                                    OnDecompress?.Invoke(zipEntry.Name, writeNum, zipEntry.Size, false);
                                    await writeStream.WriteAsync(dataBytes, 0, readNum);
                                } while (readNum > 0);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (File.Exists(unzipPath))
                    {
                        File.Delete(unzipPath);
                    }
                    else if (Directory.Exists(unzipPath))
                    {
                        Directory.Delete(unzipPath);
                    }
                    if (ex.Message.Equals("No password set."))
                    {
                        throw new ArgumentNullException("passCode", "请设置解压密码!");
                    }
                    throw ex;
                }
                finally
                {
                    zipStream.Close();
                    zipStream.Dispose();
                    Debug.Print("已成功解压压缩文件");
                }
            }
        }
示例#6
0
    internal static async void UnZipFileSync(string zipFilePath, string savePath, Action <bool, string> callback)
    {
        bool zipOk = false;

        if (!File.Exists(zipFilePath))
        {
            Console.WriteLine("Cannot find file '{0}'", zipFilePath);
            if (callback != null)
            {
                callback(false, zipFilePath);
            }
            return;
        }
        try
        {
            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry theEntry;
                zipStream.Password = KeyWord;
                while ((theEntry = zipStream.GetNextEntry()) != null)
                {
                    CurrentUnCompressSize += theEntry.Size;
                }
            }
            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry theEntry;
                zipStream.Password = KeyWord;
                CurrentUnCompress  = 0;
                while ((theEntry = zipStream.GetNextEntry()) != null)
                {
                    CurrentUnCompressIndex = theEntry.ZipFileIndex;
                    //Console.WriteLine("解压开始:" + theEntry.Name);
                    ZipName = theEntry.Name;
                    IsOk    = false;
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName      = Path.GetFileName(theEntry.Name);

                    // create directory
                    if (directoryName.Length > 0)
                    {
                        Directory.CreateDirectory(Path.Combine(savePath, directoryName));
                    }

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(Path.Combine(savePath, theEntry.Name)))
                        {
                            int    size = 1048576;
                            byte[] data = new byte[1048576];
                            while (true)
                            {
                                size = await zipStream.ReadAsync(data, 0, 1048576);

                                if (size > 0)
                                {
                                    CurrentUnCompress += size;
                                    await streamWriter.WriteAsync(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    //Console.WriteLine("------解压结束:" + theEntry.Name);
                }
            }
            zipOk = true;
        }
        catch (Exception)
        {
            zipOk = false;
            Utils.LogError("解压出错:" + zipFilePath);
        }
        if (callback != null)
        {
            callback(zipOk, zipFilePath);
        }
    }