Ejemplo n.º 1
0
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="fileToZip">要压缩的文件路径</param>
        /// <param name="zipedFile">压缩后的文件路径</param>
        public void ZipFile(string fileToZip, string zipedFile)
        {
            if (!File.Exists(fileToZip))
            {
                throw new FileNotFoundException("The specified file " + fileToZip + " could not be found.");
            }

            if (!string.IsNullOrEmpty(zipedFile))
            {
                zipedFile = Path.GetFileNameWithoutExtension(zipedFile) + ".zip";
            }

            if (Path.GetExtension(zipedFile) != ".zip")
            {
                zipedFile = zipedFile + ".zip";
            }

            string zipedDir = zipedFile.Substring(0, zipedFile.LastIndexOf("\\"));
            if (!Directory.Exists(zipedDir))
            {
                Directory.CreateDirectory(zipedDir);
            }

            using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
            {
                string fileName = Path.GetFileName(fileToZip);
                ZipEntry zipEntry = new ZipEntry(fileName);
                zipStream.PutNextEntry(zipEntry);
                zipStream.SetLevel(compressionLevel);

                using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
                {
                    int size = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, size);

                    while (size < streamToZip.Length)
                    {
                        int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="directoryToZip">要压缩的文件夹路径</param>
        /// <param name="zipedFile">压缩后的文件路径</param>
        public void ZipDerctory(string directoryToZip, string zipedFile)
        {
            if (string.IsNullOrEmpty(zipedFile))
            {
                zipedFile = directoryToZip.Substring(directoryToZip.LastIndexOf("\\") + 1);
                zipedFile = directoryToZip.Substring(0, directoryToZip.LastIndexOf("\\")) + "\\" + zipedFile + ".zip";
            }

            if (Path.GetExtension(zipedFile) != ".zip")
            {
                zipedFile = zipedFile + ".zip";
            }

            using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
            {
                ArrayList fileList = GetFileList(directoryToZip);
                int directoryNameLength = (Directory.GetParent(directoryToZip)).ToString().Length;

                zipStream.SetLevel(compressionLevel);
                ZipEntry zipEntry = null;
                FileStream fileStream = null;

                foreach (string fileName in fileList)
                {
                    zipEntry = new ZipEntry(fileName.Remove(0, directoryNameLength));
                    zipStream.PutNextEntry(zipEntry);

                    if (!fileName.EndsWith(@"/"))
                    {
                        fileStream = File.OpenRead(fileName);
                        fileStream.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }