List<string> AllFilesPath = new List<string>();
                if (filesOrDirectoriesPaths.Length > 0) // get all files path
                {
                    for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
                    {
                        if (File.Exists(@strZipTopDirectoryPath + filesOrDirectoriesPaths[i]))
                        {
                            AllFilesPath.Add(strZipTopDirectoryPath + filesOrDirectoriesPaths[i]);
                        }
                        else if (Directory.Exists(@strZipTopDirectoryPath + filesOrDirectoriesPaths[i]))
                        {
                            GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
                        }
                    }
                }

 

 for (int i = 0; i < AllFilesPath.Count; i++)
                    {
                        string strFile = AllFilesPath[i].ToString();
                        try
                        {
                            if (strFile.Substring(strFile.Length - 1) == "") //folder
                            {
                                string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                                if (strFileName.StartsWith(""))
                                {
                                    strFileName = strFileName.Substring(1);
                                }
                                ZipEntry entry = new ZipEntry(strFileName);
                                entry.DateTime = DateTime.Now;
                                zipOutputStream.PutNextEntry(entry);
                            }
                            else //file
                            {
                                FileStream fs = File.OpenRead(strFile);

                                byte[] buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);

                                string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                                if (strFileName.StartsWith(""))
                                {
                                    strFileName = strFileName.Substring(0);
                                }
                                ZipEntry entry = new ZipEntry(strFileName);
                                entry.DateTime = DateTime.Now;
                                zipOutputStream.PutNextEntry(entry);
                                zipOutputStream.Write(buffer, 0, buffer.Length);

                                fs.Close();
                                fs.Dispose();
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }

  

相关文章:

  • 2022-02-04
  • 2021-11-29
  • 2022-01-14
  • 2022-03-03
  • 2021-11-05
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-04
  • 2022-02-24
  • 2022-01-30
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案