Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt. Show Tạo File Zip sử dụng PythonĐầu tiên là import thư viện filezip import zipfile Lựa chọn mode để nén file zip # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) Tạo file Zip với đường dẫn và mode (w – write, a – append) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') Thêm file vào file zip vừa tạo # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression Close file # Don't forget to close the file! zf.close() Vậy đoạn code đầy đủ sẽ là: import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close() Kết quả Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây. Nguồn vinasupport.com Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt. Tạo File Zip sử dụng PythonĐầu tiên là import thư viện filezip import zipfile Lựa chọn mode để nén file zip # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) Tạo file Zip với đường dẫn và mode (w – write, a – append) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') Thêm file vào file zip vừa tạo # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression Close file # Don't forget to close the file! zf.close() Vậy đoạn code đầy đủ sẽ là: import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close() Kết quả Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây. Nguồn vinasupport.com Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt. Nội dung chính
Tạo File Zip sử dụng PythonĐầu tiên là import thư viện filezip import zipfile Lựa chọn mode để nén file zip # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) Tạo file Zip với đường dẫn và mode (w – write, a – append) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') Thêm file vào file zip vừa tạo # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression Close file # Don't forget to close the file! zf.close() Vậy đoạn code đầy đủ sẽ là: import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close() Kết quả Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây. Nguồn vinasupport.com Vietnamese (Tiếng Việt) translation by Dai Phong (you can also view the original English article) Nếu bạn đã từng sử dụng máy tính, bạn có thể đã bắt gặp các tập tin với phần mở rộng .zip. Chúng là các tập tin đặc biệt có thể chứa nội dung được nén của nhiều tập tin, thư mục và thư mục con khác. Điều này giúp chúng được truyền tải trên trên internet một cách dễ dàng. Bạn có biết rằng bạn có thể sử dụng Python để nén hoặc giải nén tập tin? Bài này sẽ hướng dẫn bạn cách làm thế nào để sử dụng mô-đun zipfile trong Python, để giải nén hoặc nén từng tập tin hay nhiều tập tin cùng một lúc. Nén các Tập tin Riêng lẻCái này thì dễ và đòi hỏi rất ít code. Chúng ta bắt đầu bằng cách import mô-đun zipfile và sau đó mở đối tượng ZipFile trong chế độ ghi bằng cách chỉ định tham số thứ hai là 'w'. Tham số đầu tiên là đường dẫn đến chính tập tin. Đây là code mà bạn cần: import zipfile jungle_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\jungle.zip', 'w') jungle_zip.write('C:\\Stories\\Fantasy\\jungle.pdf', compress_type=zipfile.ZIP_DEFLATED) jungle_zip.close() Xin lưu ý rằng tôi sẽ chỉ định đường dẫn trong tất cả các đoạn code theo một định dạng kiểu Windows; bạn sẽ cần phải thay đổi phù hợp nếu bạn đang ở trên Linux hoặc Mac. Bạn có thể chỉ định các phương thức nén khác nhau để nén các tập tin. Các phương thức mới hơn Nén nhiều Tập tinĐiều này hơi phức tạp một chút vì bạn cần lặp qua tất cả các tập tin. Code dưới đây sẽ nén tất cả các tập tin với phần mở rộng tập tin pdf trong một thư mục nhất định: import os import zipfile fantasy_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\archive.zip', 'w') for folder, subfolders, files in os.walk('C:\\Stories\\Fantasy'): for file in files: if file.endswith('.pdf'): fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), 'C:\\Stories\\Fantasy'), compress_type = zipfile.ZIP_DEFLATED) fantasy_zip.close() Lần này, chúng ta đã import mô-đun Nếu bạn không muốn giữ lại cấu trúc thư mục, bạn có thể gom tất cả các tập tin với nhau bằng cách sử dụng dòng code sau: fantasy_zip.write(os.path.join(folder, file), file, compress_type = zipfile.ZIP_DEFLATED) Phương thức Giải nén tất cả các Tập tinBạn có thể sử dụng phương thức import zipfile fantasy_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\archive.zip') fantasy_zip.extractall('C:\\Library\\Stories\\Fantasy') fantasy_zip.close() Nếu bạn muốn giải nén nhiều tập tin, bạn sẽ phải cung cấp tên của các tập tin mà bạn muốn giải nén dưới dạng một danh sách. Giải nén từng Tập tinĐiều này cũng tương tự như giải nén nhiều tập tin. Một sự khác biệt là lần này, bạn cần phải cung cấp tên tập tin đầu tiên và đường dẫn để giải nén chúng vào đó. Ngoài ra, bạn cần phải sử dụng phương thức import zipfile fantasy_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\archive.zip') fantasy_zip.extract('Fantasy Jungle.pdf', 'C:\\Stories\\Fantasy') fantasy_zip.close() Đọc các Tập tin ZipHãy xem xét một trường hợp mà bạn cần xem một tập tin lưu trữ zip có chứa một tập tin cụ thể nào đó hay không. Đến thời điểm này, lựa chọn duy nhất của bạn để làm điều đó là bằng cách giải nén tất cả các tập tin trong kho lưu trữ. Tương tự, bạn có thể cần phải giải nén chỉ những tập tin đó mà lớn hơn một kích thước cụ thể. Mô-đun Sử dụng phương thức
Gọi phương thức Bạn cũng có thể trực tiếp đọc nội dung của một tập tin cụ thể từ tập tin nén bằng cách sử dụng phương thức Để có được kích thước nén của một tập tin riêng biệt từ tập tin nén, bạn có thể sử dụng thuộc tính Code sau đây sử dụng các thuộc tính và phương thức mà chúng ta vừa mới thảo luận để giải nén chỉ những tập tin có kích thước dưới 1MB. import zipfile stories_zip = zipfile.ZipFile('C:\\Stories\\Funny\\archive.zip') for file in stories_zip.namelist(): if stories_zip.getinfo(file).file_size < 1024*1024: stories_zip.extract(file, 'C:\\Stories\\Short\\Funny') stories_zip.close() Để biết ngày và giờ khi một tập tin cụ thể từ tập tin nén bị sửa đổi gần
đây nhất, bạn có thể sử dụng thuộc tính import zipfile stories_zip = zipfile.ZipFile('C:\\Stories\\Funny\\archive.zip') thirsty_crow_info = stories_zip.getinfo('The Thirsty Crow.pdf') print(thirsty_crow_info.date_time) print(thirsty_crow_info.compress_size) print(thirsty_crow_info.file_size) stories_zip.close() Thông tin về kích thước tập tin gốc và kích thước tập tin nén có thể giúp bạn quyết định có nên nén một tập tin hay không. Tôi chắc rằng nó cũng có thể được sử dụng trong một số trường khác. Tổng kếtRõ ràng, sử dụng mô-đun Thật tình, tôi thấy cũng khá thú vị khi nén và giải nén tập tin bằng cách viết code của riêng mình. Tôi hy vọng bạn thích hướng dẫn này, và nếu bạn có bất kỳ câu hỏi nào, xin vui lòng cho tôi biết trong phần bình luận nhé. |