-
-
Save PradeepJaiswar/c136656784bca626a9c9fe2ee5bf4cf5 to your computer and use it in GitHub Desktop.
Zip folder files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, os.path | |
import tarfile | |
# constants | |
zipNamePath = "/Users/pradeep/workspace/upstox/code/python/zipped_files/" | |
searchDirectory = "/Users/pradeep/workspace/upstox/code/python/zip-to-file" | |
maxZipFileSize = 1000 #in megabytes | |
zipListDict = {} | |
zipList = [] | |
totalSize = 0 | |
zipCount = 0 | |
def zipFiles(zipList, key): | |
zipName = zipNamePath + "zip_" + str(key) + ".tar.gz" | |
print("Compression started for ", len(zipList)) | |
tar = tarfile.open(zipName, "w:gz") | |
for name in zipList: | |
fileName = os.path.basename(name) | |
tar.add(name, arcname=fileName) | |
tar.close() | |
print("Compression done for ", len(zipList), " files at ", zipName) | |
return zipName | |
def prepareFilesForZip(): | |
global maxZipFileSize | |
global zipListDict | |
global zipList | |
global totalSize | |
global zipCount | |
# Set the root, directory, and file name | |
for root,direc,f in os.walk(searchDirectory): | |
#Go through the files in directory | |
for name in f: | |
# ignore the hidden files | |
if (name[0] == '.'): | |
continue | |
fullName = os.path.join(root,name) | |
# Split the file name from the file extension | |
n, ext = os.path.splitext(name) | |
# Get size of each file in directory, size is obtained in BYTES | |
fileSize = os.path.getsize(fullName) | |
fileSizeInMb = float(fileSize)/float(1048576) | |
if(fileSizeInMb >= maxZipFileSize): | |
zipCount +=1 | |
zipListDict[zipCount] = zipList | |
zipList = [] | |
zipList.append(fullName) | |
totalSize = 0 | |
else: | |
# Add up the total sizes for all the files in the directory | |
totalSize += fileSize | |
totalSizeInmegabytes = float(totalSize)/float(1048576) | |
if totalSizeInmegabytes >= maxZipFileSize: | |
zipCount +=1 | |
zipListDict[zipCount] = zipList | |
zipList = [] | |
zipList.append(fullName) | |
totalSize = fileSize | |
else: | |
zipList.append(fullName) | |
if (len(zipListDict) == 0 and len(zipList) > 0): | |
zipCount +=1 | |
zipListDict[zipCount] = zipList | |
return zipListDict | |
def startZipProcess(dict): | |
if len(dict) > 0: | |
for key in dict: | |
print(dict[key]) | |
print("Sending ", len(dict[key]), " files for compression") | |
zipFiles(dict[key], key) | |
else: | |
print("No file found for compression") | |
def main(): | |
zipFilesList = prepareFilesForZip() | |
startZipProcess(zipFilesList) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment