# Python 文件夹压缩

使用 shutil 对文件夹进行zip压缩，压缩过程显示进度条

```python
# -*- coding: UTF-8 -*-
import os
import shutil
import logging
from progress.bar import IncrementalBar
logger = logging.getLogger(__name__)

def count_files_in_dir(dir):
    totalFiles = 0
    for base, dirs, files in os.walk(dir):
        totalFiles += len(files)
    return totalFiles

def zip_with_progress(dir_path, zip_file):
    bar = None
    total_files = count_files_in_dir(dir_path)

    def progress(*args, **kwargs):
        # TODO(You): 进度条显示
    
    # 调用shutil.make_archive时，临时替换其 logger 参数，用来显示进度条
    old_info = logger.info
    logger.info = lambda *args, **kwargs: progress(*args, **kwargs)
    shutil.make_archive(dir_path, 'zip', dir_path, logger=logger)
    logger.info = old_info

    if bar is not None:
        bar.finish()

if __name__ == '__main__':
    zip_with_progress('./', '/tmp/test_file_zip.zip')
    print()
```

请选出下列能**正确**实现这一功能的选项。

## template

```python
import os
import shutil
import logging
from progress.bar import IncrementalBar
logger = logging.getLogger(__name__)


def count_files_in_dir(dir):
    totalFiles = 0
    for base, dirs, files in os.walk(dir):
        totalFiles += len(files)
    return totalFiles


def zip_with_progress(dir_path, zip_file):
    bar = None
    total_files = count_files_in_dir(dir_path)

    def progress(*args, **kwargs):
        if not args[0].startswith('adding'):
            return

        nonlocal bar, total_files
        if bar is None:
            print('@开始压缩:{}'.format(zip_file))
            bar = IncrementalBar('正在压缩:', max=total_files)
        bar.next(1)

    old_info = logger.info
    logger.info = lambda *args, **kwargs: progress(*args, **kwargs)

    shutil.make_archive(dir_path, 'zip', dir_path, logger=logger)
    logger.info = old_info

    if bar is not None:
        bar.finish()

if __name__ == '__main__':
    zip_with_progress('./', '/tmp/test_file_zip.zip')
    print()
```

## 答案

```python
def progress(*args, **kwargs):
    if not args[0].startswith('adding'):
        return

    nonlocal bar, total_files
    if bar is None:
        print('@开始压缩:{}'.format(zip_file))
        bar = IncrementalBar('正在压缩:', max=total_files)
    bar.next(1)
```

## 选项

### A

```python
def progress(*args, **kwargs):
    if not args[0].startswith('adding'):
        return

    if bar is None:
        print('@开始压缩:{}'.format(zip_file))
        bar = IncrementalBar('正在压缩:', max=total_files)
    bar.next(1)
```

### B

```python
def progress(*args, **kwargs):
    nonlocal bar, total_files
    if bar is None:
        print('@开始压缩:{}'.format(zip_file))
        bar = IncrementalBar('正在压缩:', max=total_files)
    bar.next(1)
```

### C

```python
def progress(*args, **kwargs):
    if not args[0].startswith('adding'):
        return

    nonlocal bar, total_files
    print('@开始压缩:{}'.format(zip_file))
    bar = IncrementalBar('正在压缩:', max=total_files)
    bar.next(1)
```
