1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3でネスト化されたZipを解凍する

Last updated at Posted at 2019-05-12

###2019/10/02 改良版のコードを記載
###Python3で2重ネストされたZipファイルを解凍する方法の備忘録
初投稿です。仕事でPythonを使ってデータの成型をしたいと思う機会が増えたため投稿を始めました。よろしくお願いします。

#####ZipFileモジュールで解凍するが、ネストされたZipファイルの解凍メソッドがないため、自作
なお、3重ネスト以上のZipファイルはmain.py部分を参照

#1.開発環境

  • Windows10
  • Python3.7.3

#2.対象Zipファイル

work/
    zip_nesting.zip
     ├ zip1.zip
     │    └ text1.txt
     │    └ text2.txt
     ├ zip2.zip
     │    └ text3.txt
     ∟    └ text4.txt

#3.結果(展開後のworkディレクトリ)

work/
    text1.txt
    text2.txt
    text3.txt
    text4.txt
    zip1.zip
    zip2.zip
    zip_nesting.zip

#4.コード

main.py
import glob
import os
from zipfile import ZipFile

def f_GetZipFileList(arg_path , arg_files=0):
    from glob import glob
    import os

    if arg_files == 0:
        files = glob(os.path.join(arg_path , '*.zip'))
        return files
    else:
        #Calculate Diff "Extracted Zip Files" and "Exist Zip Files" 
        files = glob(os.path.join(arg_path , '*.zip'))
        files_difset = list(set(files) - set(arg_files))
        return files_difset



def f_ExtractZip(arg_path , arg_files):
    from zipfile import ZipFile

    for file in arg_files:
        with ZipFile(file) as zip:
            zip.extractall(arg_path)


dstpath = "./work"
path = "./work/**"

#zipファイルなら解凍、zip以外なら何もしない
for file in glob.glob(dstpath + "/*.zip"):
        with ZipFile(file) as zip:
                zip.extractall(dstpath)

#全てのサブフォルダを対象にzipファイルを展開する
for file in glob.glob(path + "/*.zip"):
        with ZipFile(file) as zip:
                zip.extractall(dstpath)

#5.参考にしたサイト
####Pythonでzipファイルを圧縮・解凍するzipfile
#####https://note.nkmk.me/python-zipfile/

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?