Skip to main content

Working with Zip file in python(writing)

                     Working with Zip File in Python(Writing)
            
Module in Python to write zip file:   
                 The zipfile module can be used to manipulate ZIP archive files.Sometimes it is necessary to write 
                to a ZIP archive using data that did not come from an existing file. Rather than writing the data to 
                a file, then adding that file to the  ZIP archive, you can use the writestr() method to add a string 
                of bytes to the archive directly.

          Code:   
                           from zipfile_infolist import print_info
                           import zipfile

                           msg = 'This data did not exist in a file before being added to the ZIP file'
                           zf = zipfile.ZipFile('zipfile_writestr.zip', mode='w', compression=zipfile.ZIP_DEFLATED,) 
                          try:
                               zf.writestr('from_string.txt', msg)
                          finally:
                              zf.close()
                          print_info('zipfile_writestr.zip'
                          zf = zipfile.Zip File('zipfile_writestr.zip', 'r')
                          print zf.read('from_string.txt')
Output:      
                    $ python zipfile_writestr_zipinfo.py
                    from_string.txt
                    Comment:        Remarks go here
                    Modified:       2019-11-14 11:44:14
                    System:         0 (10 = Windows)
                    ZIP version:    20
                    Compressed:     62 bytes
                    Uncompressed:   64 bytes           
A directory with such a format:
                          # importing required modules
from zipfile import ZipFile
import os
  
def get_all_file_paths(directory):
  
     # initializing empty file paths list
     file_paths = []
  
     # crawling through directory and subdirectories
     for root, directories, files in os.walk(directory):
         for filename in files:
             # join the two strings in order to form the full filepath.
             filepath = os.path.join(root, filename)
             file_paths.append(filepath)
  
     # returning all file paths
     return file_paths        
  
def main():
     # path to folder which needs to be zipped
     directory = './python_files'
  
     # calling function to get all file paths in the directory
     file_paths = get_all_file_paths(directory)
  
     # printing the list of all files to be zipped
     print('Following files will be zipped:')
     for file_name in file_paths:
         print(file_name)
  
     # writing files to a zipfile
     with ZipFile('my_python_files.zip','w') as zip:
         # writing each file one by one
         for file in file_paths:
             zip.write(file)
  
     print('All files zipped successfully!')        
  
  
if __name__ == "__main__":
     main()

Output:
Following files will be zipped:
./python_files\python_basic.pdf
./python_files\python_wiki.text
./python_files\logos\python_logos.jpg
All files zipped successfully!
Explanation:
                          def get_all_file_paths(directory):
                                   file_paths = []

                                   for root, directories, files in os.walk(directory):
                                           for filename in files:
                                               filepath = os.path.join(root, filename)
                                               file_paths.append(filepath)

                                  return file_paths                              
               

Popular posts from this blog

Introduction to Java Security

Introduction to Java Security The Java security architecture includes a large set of application programming interfaces (APIs), tools, and implementations of commonly-used security algorithms, mechanisms, and protocols. The Java security APIs span a wide range of areas. Cryptographic and public key infrastructure (PKI) interfaces provide the underlying basis for developing secure applications. Interfaces for performing authentication and access control enable applications to guard against unauthorized access to protected resources. The JDK includes a number of providers that implement a core set of security services. It also allows for additional custom providers to be installed. This enables developers to extend the platform with new security mechanisms. The JDK is divided into modules. Modules that contain security APIs include the following:

Module Description java.base Defines the foundational APIs of Java SE;  contained packages include java.securityjavax.cryptojavax.net.ssl,  and…

SQL Injection

Overview A SQL injection attack consists of insertion or "injection" of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands. Threat ModelingSQL injection attacks allow attackers to spoof identity, tamper with existing data, cause repudiation issues such as voiding transactions or changing balances, allow the complete disclosure of all data on the system, destroy the data or make it otherwise unavailable, and become administrators of the database server.SQL Injection is ve…

Insertion Node in the Linkelist.

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list
public class Linkedlist { Node head; class Node{ int data; Node next; Node(int d){ data =d; next=null; } } // INSERT THE NODE AT THE BEGIN OF LINKEDLIST. public void insertAtfront(int new_data){ // Node temp = head; Node new_node = new Node(new_data); new_node.next = head; head = new_node; }  // INSERT THE NODE AT THE GIVEN POSITION IN LINKEDLIST.
public void insertAtGiven(Node prev_node,int new_data) { if(prev_node == null){ System.out.print("previous node can't be null"); return; } Node new_node = new Node(new_data); new_node.next = prev_node.next; prev_node.next = new_node; } // INSERT THE NODE  AT THE END OF THE LINKEDLIST.   public void insertAtEnd(int new_data){ Node new_node = new Node(new_data); new_node.nex…