473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Archiving files with zipfile.ZipFile

bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. the_records = []
  2.  
  3. for record in tick:
  4.     query = 'select location from download_file where d_id = %s' %record
  5.     print "<br>", query
  6.     curs.execute(query)
  7.     a= curs.fetchone()
  8.     print "<br>", a[0]
  9.     the_records.append(a[0])
  10.  
  11. print "<br>To be zipped: ", the_records
  12.  
  13. def zipdir():
  14.     for b in the_records:
  15.         print "<br>In record: ", b
  16.         try:
  17.             zip.write(the_records + b)
  18.         except IOError:
  19.             None
  20. if __name__=='__main__':
  21.  
  22.     zip = zipfile.ZipFile("C:\\zipfile.zip", 'w')
  23.     zipdir('')
  24.     zip.close
Following is an example that may provide a solution to you:
Expand|Select|Wrap|Line Numbers
  1. """
  2. Function makeArchive is a wrapper for the Python class zipfile.ZipFile
  3. 'fileList' is a list of file names - full path each name
  4. 'archive' is the file name for the archive with a full path
  5. """
  6.  
  7. import zipfile, os
  8.  
  9. def makeArchive(fileList, archive):
  10.     try:
  11.         # ZipFile will accept a file name or file object
  12.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  13.         for f in fileList:
  14.             print "archiving file %s" % (f)
  15.             a.write(f, os.path.basename(f))
  16.         a.close()
  17.         return True
  18.     except: return False
  19.  
  20. if __name__== '__main__':
  21.  
  22.     fileList = ['H:/TEMP/temsys/Anchor Rod Plans.txt',
  23.                 'H:/TEMP/temsys/abc1.txt',
  24.                 'H:/TEMP/temsys/abc2.txt',
  25.                 'H:/TEMP/temsys/abc2.txt',
  26.                 'H:/TEMP/temsys/abc3.txt',
  27.                 'H:/TEMP/temsys/abc4.txt'
  28.                 ]
  29.  
  30.     arcfile_name = 'H:/TEMP/temsys/zipped_files.zip'
  31.  
  32.     if makeArchive(fileList, arcfile_name):
  33.         print arcfile_name, "was created.\n"
  34.         # check the new archive file
  35.         f = zipfile.ZipFile(arcfile_name, 'r')
  36.         for info in f.infolist():
  37.             print info.filename, info.date_time, info.file_size, info.compress_size
  38.         f.close()
  39.     else:
  40.         print "There was an error"
  41.  
  42. """Sample Output
  43. >>> archiving file H:/TEMP/temsys/Anchor Rod Plans.txt
  44. archiving file H:/TEMP/temsys/abc1.txt
  45. archiving file H:/TEMP/temsys/abc2.txt
  46. archiving file H:/TEMP/temsys/abc2.txt
  47. archiving file H:/TEMP/temsys/abc3.txt
  48. archiving file H:/TEMP/temsys/abc4.txt
  49. H:/TEMP/temsys/zipped_files.zip was created.
  50.  
  51. Anchor Rod Plans.txt (2005, 10, 19, 19, 17, 16) 1657 828
  52. abc1.txt (2007, 1, 5, 11, 16, 46) 61 46
  53. abc2.txt (2007, 1, 5, 11, 1, 2) 43 41
  54. abc2.txt (2007, 1, 5, 11, 1, 2) 43 41
  55. abc3.txt (2007, 1, 5, 11, 0, 46) 43 39
  56. abc4.txt (2007, 1, 5, 11, 0, 30) 25 25
  57. >>> """
HTH :-)
Jan 19 '07 #1
3 4890
cuties
8 New Member
thanks..... i'll try this example..... here is another code that i did.....i hope you could comment on it..... as i told you before there is checkbox something like our email and wen a button is pushed after the checkbox is ticked....it'll download the files from its location and created a zip file....so here is the codes if more that 1 box is checked or if only 1 box is checked......

Expand|Select|Wrap|Line Numbers
  1. c = len(str(tick[0])) 
  2.  
  3. if c > 1:
  4.  
  5.     print "<p>The d_id that is checked is:" , tick, "</p>"
  6.  
  7.     the_records = []
  8.  
  9.     for record in tick:
  10.  
  11.     query = 'select location from download_file where d_id = %s' %record
  12.     print "<br>", query 
  13.     curs.execute(query)
  14.     a= curs.fetchone()
  15.     print "<br>", a[0]
  16.     the_records.append(a[0])
  17.  
  18.     print "<p>To be zipped: ", the_records, "</p>"
  19.  
  20.     zip = zipfile.ZipFile("C:\\zipfile.zip", 'w')
  21.  
  22.     for each in the_records:
  23.  
  24.     print "<p> Each: " , each, "</p>" 
  25.     try:
  26.         zip.write(each)
  27.     except IOError:
  28.         None
  29.  
  30.  
  31.     zip.close()
  32.  
  33. elif c == 1:
  34.  
  35.     the_records = []
  36.  
  37.     print "<p>The d_id that is checked is:" , tick, "</p>"
  38.  
  39.     query = 'select location from download_file where d_id = %s' %tick
  40.  
  41.     print "<p>", query, "</p>"
  42.  
  43.     curs.execute(query)
  44.     a= curs.fetchone()
  45.     print "<br>", a[0]
  46.     the_records.append(a[0])
  47.  
  48.     print "<p>To be zipped: ", the_records, "</p>"
  49.  
  50.     zip = zipfile.ZipFile("C:\\Temp\\zipfile.zip", 'w')
  51.  
  52.     for each in the_records:
  53.  
  54.     print "<p> Each: " , each, "</p>"
  55.     try:
  56.         zip.write(each)
  57.     except IOError:
  58.         None
  59.  
  60.     zip.close()
  61.  
but now my problem is that each time da button is pushed...it overwrites the old zip file...so i don't know how could i avoid this....pls help with this.....and don't forget to comment on this new codes....thanks for helping so much.....
Jan 22 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
Three things come to mind -
Ask the user for a zip file name.
Check to see if the zip file exists (os.path.isfile (path_file)). If it does, change the filename to something else like zipfile1.zip or zipfile2.zip.
Instantiate zipfile.ZipFile in append mode ('a' instead of 'w'). All files will be added to the same file.

Sorry I cannot be more help with CGI/HTML issues.
Jan 22 '07 #3
cuties
8 New Member
Thanks..... will give it a try but i don't want the user to give an input as this will hv 2 b in another cgi then which i would like to avoid......than ks once again for really helpin me out.....
Jan 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
1881
by: Oriana | last post by:
Hi! I'm beginning to use the zipfile module in Python and I'm confused about something. I am trying to extract certain files from one zip and copy them into another one. This is the code I‘ve got so far: import string import os, re
1
1539
by: iamlevis3 | last post by:
Does Python have any internal facility for creating recursive archives of a directory? I'd like to avoid reliance on extenal tools (winzip,tar,etc). Thanks!
5
2127
by: OriginalBrownster | last post by:
This will probably sound like a very dumb question. I am trying to zip some files within a directory. I want to zip all the files within a directory called "temp" and have the zip archive saved in a directory with temp called ziptemp I was trying to read up on how to use the zipfile module python provides, but I cannot seem to find adequate documentation on function itself.
1
1198
by: bahoo | last post by:
Hi, Can I use python to recursively compress files under subdirectories with a certain format such as "ABC_XXX_XXX.dat" into a .gz or .zip file? I used to do it with "tar" on unix, but I don't like to put commands into a single line, as it is often more prone to error. Thanks bahoo
5
2227
by: techusky | last post by:
I made a script that successfully creates a .zip file of all the files in a directory on my web server, but now what I haven't figured out how to do is how to have it automatically deleted when the user successfully downloads it, as otherwise my server would eventually get clogged up with all these zip files. Any help/suggestions? Thanks
2
5453
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it is several GB in size. When it comes time to read the 5+GB file from inside the zip file, it fails with the following error: File "...\zipfile.py", line 491, in read bytes = self.fp.read(zinfo.compress_size) OverflowError: long it too large to...
0
2070
by: avik1612 | last post by:
Hi, I have created a program to unzip the file. It is unzipping the zip files when put in directory i.e folder inside folder but when i create a zip file without putting in a folder and try to unzip it. It unzips it but put the zipped file outside the folder created in the name of the zip file. I want it to be inside it. Also when i do beyond compare of the two files i.e the zipped file and original file its shows difference. What...
3
4323
by: dp_pearce | last post by:
Hi all, I have come across an error while using zipfile and I can't seem to find somewhere that explains the problem. My script needs to be able to take text files from one drive and add them to zip files on another drive. The following seems to work just fine. import zipfile # write test file in working directory directory
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.