循环压缩某个目录

首先下载ICSharpCode.SharpZipLib.dll,然后添加应用

接着建类 代码如下

/// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="args">源目录 压缩成的文件</param>
    public void ZipFileMain(string[] args)
    {
        string[] filenames = Directory.GetFiles(args[0]);

        Crc32 crc = new Crc32();
        ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));

        s.SetLevel(6); // 0 - store only to 9 - means best compression

       
            yasuo(args[0], args[0], false, crc, s);
       

        s.Finish();
        s.Close();
    }

    /// <summary>
    /// 循环压缩
    /// </summary>
    /// <param name="souce">源目录</param>
    /// <param name="str">文件名或目录名</param>
    /// <param name="isFile">是文件还是目录</param>
    /// <param name="crc"></param>
    /// <param name="s"></param>
    public void yasuo(string souce, string str, bool isFile, Crc32 crc, ZipOutputStream s)
    {
        if (isFile)
        {
            try
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(str);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                //FileInfo info = new FileInfo(file);
                //压缩后显示的路径为当前压缩的目录;
                ZipEntry entry = new ZipEntry(str.Replace(souce, ""));

                //ZipEntry   entry   =   new   ZipEntry(file); //压缩后显示全路径
                entry.DateTime = DateTime.Now;

                // set Size and the crc, because the information
                // about the size and crc should be stored in the header
                // if it is not set it is automatically written in the footer.
                // (in this case size == crc == -1 in the header)
                // Some ZIP programs have problems with zip files that don't store
                // the size and crc in the header.
                entry.Size = fs.Length;
                fs.Close();

                crc.Reset();
                crc.Update(buffer);

                entry.Crc = crc.Value;

                s.PutNextEntry(entry);

                s.Write(buffer, 0, buffer.Length);
            }
            catch { }
           
        }else{
            //当前目录下的目录
            foreach (string d in Directory.GetDirectories(str))
            {
                foreach (string file in Directory.GetFiles(d))
                {
                    yasuo(souce, file, true, crc, s);

                }
                foreach (string dd in Directory.GetDirectories(d))
                {
                    yasuo(souce, dd, false, crc, s);
                }
            }
            //当前目录下的文件
            foreach (string file in Directory.GetFiles(str))
            {
                yasuo(souce, file, true, crc, s);
            }
        }
    }

 

 

具体调用如下

 string[] FileProperties = new string[2];

        FileProperties[0] = Server.MapPath("~/");//待压缩文件目录

        FileProperties[1] = Server.MapPath("~/") + "\\备份.zip";  //压缩后的目标文件

        clsZip Zc = new clsZip();

        Zc.ZipFileMain(FileProperties);

        Response.Write("<script language=javascript>alert('备份成功!')</script>");

 

参考文章  http://blog.csdn.net/qhq5152096/article/details/3863921

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用下面的代码来遍历某个目录下的所有文件,并找到不是今天创建的文件,并将其压缩: ``` #!/bin/bash # 遍历目录中的所有文件 for file in /path/to/directory/* do # 获取文件的创建时间 create_time=$(stat -c %y "$file" | cut -d' ' -f1) # 获取今天的日期 today=$(date +%Y-%m-%d) # 如果文件不是今天创建的,则进行压缩 if [ "$create_time" != "$today" ] then gzip "$file" fi done ``` 需要注意,这段代码假设文件的创建时间是文件系统中保存的,如果文件系统不保存文件的创建时间,则无法使用这段代码。 此外,还可以使用其他命令来获取文件的修改时间,比如 `stat -c %y` 和 `find` 命令。 希望这能帮到您! ### 回答2: 为了回答这个问题,我们可以编写一个shell脚本来遍历指定目录下的所有文件,并找到不是当天创建或修改的文件。然后,我们可以使用压缩工具将这些文件进行压缩。 脚本如下: ```shell #!/bin/bash target_dir="/path/to/directory" # 指定目标目录路径 compress_dir="/path/to/compress" # 指定压缩后文件存放路径 # 遍历目标目录下的所有文件 for file in "$target_dir"/*; do # 检查文件最近修改日期是否是今天 if [ $(stat -c %Y $file) -lt $(date -d 'today 00:00' +%s) ]; then # 压缩文件并将其移动到压缩目录filename=$(basename "$file") # 提取文件名 compressed_file="$compress_dir/$filename.tar.gz" # 压缩后的文件路径 tar -czf "$compressed_file" "$file" # 压缩文件 echo "已压缩文件: $file" # 如果需要删除原文件,可以添加以下代码 # rm "$file" fi done ``` 请将`/path/to/directory`替换为你想要遍历的目标目录的实际路径,将`/path/to/compress`替换为你想要保存压缩文件的实际路径。 脚本首先使用一个循环来遍历目标目录下的所有文件。接下来,我们使用`stat -c %Y`命令获取文件的最近修改日期。然后,我们将该日期与今天的日期进行比较,如果小于今天的日期,则表示该文件不是今天创建或修改的。 如果文件满足条件,我们使用`tar -czf`命令将其压缩为一个tar.gz文件,并将该文件移动到一个指定的压缩目录。你还可以根据需要添加代码,在压缩后删除原文件。 这只是一个简单的示例,你可以根据自己的需求进一步修改和优化脚本。 ### 回答3: 下面是一个遍历目录下所有文件,找到修改时间不是今天的文件,并进行压缩的shell程序。假设目标目录为"/path/to/directory": ```shell #!/bin/bash # 设置目标目录 directory="/path/to/directory" # 获取今天的日期 today=$(date +"%Y-%m-%d") # 遍历目录下所有文件 for file in "$directory"/*; do # 检查文件是否是普通文件 if [ -f "$file" ]; then # 获取文件的修改时间并转换为日期格式 file_date=$(date -r "$file" +"%Y-%m-%d") # 检查文件的修改时间是否为今天之前 if [[ "$file_date" != "$today" ]]; then # 压缩文件 gzip -c "$file" > "$file.gz" fi fi done ``` 解释: 1. 首先,我们设置目标目录为变量`directory`,这样可以方便后续修改目录路径。 2. 然后,使用`date`命令获取今天的日期,并保存在变量`today`中。 3. 接着,使用`for`循环遍历目录下的所有文件,保存在变量`file`中。 4. 使用`if`语句检查`file`是否是普通文件,避免处理目录和其他特殊文件。 5. 使用`date`命令获取文件的修改时间,并转换为日期格式,保存在变量`file_date`中。 6. 使用`if`语句检查`file_date`是否等于`today`,如果不等于表示文件不是今天修改的。 7. 最后,使用`gzip`命令将文件压缩为`.gz`格式,并保存在同一目录下,文件名为原文件名加上`.gz`后缀。 你可以将上述代码保存为一个shell脚本文件(例如`compress.sh`),然后通过终端运行该脚本即可对目标目录下的文件进行压缩
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值