none
How to create zip archive using foreach statement and SharpZipLib library.

    Question

  • Hello!

    I am trying to create an archive with specific files which I have in the same foldef(vild card like this: "T_D_FD_DRY").

    Also I want to create archive only on the first day of every month so I get date from files.

    Help me plz to finish my code.

    Thanks a lot in advance!

    public void Main()
    		{
                string srsFolder = @"source folder";
                string dstFolder = @"Destination folder";
                //string name = @;
                string[] fileList = System.IO.Directory.GetFiles(srsFolder,"files mask*" );
                foreach (var s in fileList)
                {
                    //File.Move(srsFolder, dstFolder);
                    var sourceFile = new FileInfo(s);
                    sourceFile.CopyTo(dstFolder + "\\" + s.Replace(srsFolder, string.Empty),true);
                }
    
                foreach (var s in Directory.EnumerateFiles(srsFolder,"files mask*"))
                {
                    File.Delete(s);
                }
    
    			Dts.TaskResult = (int)ScriptResults.Success;
    		}

    Sunday, April 23, 2017 5:00 PM

All replies

  • Hi AndreyJones,

    Please modify file like this:

    static void Main(string[] args)
            {
                string srsFolder = @"source folder";
                string dstFolder = @"Destination folder";
                //string name = @;
                string[] fileList = System.IO.Directory.GetFiles(srsFolder, "files mask*");
                foreach (var s in fileList)
                {
                    //File.Move(srsFolder, dstFolder);
                    var sourceFile = new FileInfo(s);
                    sourceFile.CopyTo(dstFolder + "\\" + s.Replace(srsFolder, string.Empty), true);
                }
                
                // zip up the files
                try
                {
                    // Zip up the files - From SharpZipLib Demo Code
                    using (ZipOutputStream s = new ZipOutputStream(File.Create("c:\\temp\\test.zip")))
                    {
                        s.SetLevel(9); // 0-9, 9 being the highest compression
                        byte[] buffer = new byte[4096];
                        foreach (string file in fileList)
                        {
                            ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                            entry.DateTime = DateTime.Now;
                            s.PutNextEntry(entry);
                            using (FileStream fs = File.OpenRead(file))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0,
                                    buffer.Length);
    
                                    s.Write(buffer, 0, sourceBytes);
    
                                } while (sourceBytes > 0);
                            }
                        }
                        s.Finish();
                        s.Close();
                    }
    
                }
                catch (Exception ex)
                {
                }
    
                foreach (var s in Directory.EnumerateFiles(srsFolder, "files mask*"))
                {
                    File.Delete(s);
                }
    
                //Dts.TaskResult = (int)ScriptResults.Success;
            }

    Best regards,

    Cole Wu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    Monday, April 24, 2017 9:43 AM
    Moderator