Name a folder when zipping a file in node - node.js

I am creating a zip file with JSZip but I have a very specific requirement that I can't figure out how to do.
When this file is unzipped it needs to create a folder with two files inside it. Let's say foo.zip unzips to create a folder foo with files bar.mp4 and baz.png.
So far that's easy enough I can just go
let zip = new Zip()
let folder = zip.folder('foo')
folder.file(`bar.mp4`, videoData)
folder.file(`baz.png`, imageData)
// Save as foo.zip
But what I need to happen is if I rename foo.zip to horse.zip then the folder created should be called horse. If I use the above code then it will always be called foo. I know this is possible because I have several zip files that currently do this.
Edit: I'm not tied to JSZip here either. I'm happy to do this any way that's possible. Code will only be run on the server.

Related

Importing a whole folder of python files

In the current python program I'm working on, I need to access a lot of stored data. I store it in the form of a bunch of dictionaries, each in their own file. Each file has a single command: giveArchive(). So to access one of the files, I use:
import fileName
return fileName.giveArchive()
And this has worked well so far, but as the number of files I need grows, I want to streamline this a little bit. I'd like to store all of these files in the same folder, and that folder in the same directory as my main file. Is there some way I can import every file in a folder? And if I do, how can I use 'giveArchive()' from specific files in it?
You can do something like:
from folder.subfolder.deepersubfolder import filename
return filename.giveArchive()
this assumes folder can be accessed from the directory your script is running in

How do I create a bucket and multiple subfolders at once with Boto3 in s3?

I'm able to create one directory at the top level using
s3.create_bucket(Bucket=bucket_name)
I want to create a new bucket and subfolders so I have a directory structure like:
-top_level_bucket
-sub_folder
-sub_sub_folder
I want to do something like this to create everything at once if not already existent:
path = 'top_level_bucket/sub_folder/sub_sub_folder'
s3.create_bucket(Bucket=path)
Is this possible?
There is no concept of a 'sub-bucket' in Amazon S3.
Amazon S3 is actually a flat object storage service. It does not use directories.
Instead, files are uploaded with a path, eg:
aws s3 cp file.txt s3://my-bucket/bob/files/file.txt
The full name of the object will be: bob/files/file.txt
It looks and behaves like there are directories, but they are not actually there. In fact, you can run the above command and it will automatically 'create' the bob and files directory, but they are not actually there! If you delete the object, those directories will disappear (because they were never actually there!).
Bottom line: Upload files to where ever you wish, even if the buckets do not exist. Don't worry about creating a folder structure in advance.

How do you move the contents of a randomly named folder to a new location when you dont know what the folder name will be?

I am using requests to download an image with python. That part works ok. When I download a file I provide a name: sitea.zip . When that file is decompressed it contains a folder with a random name, something like ZX234564563SDSD, that has a qcow2 image in it named gw-vm.qcow2.
I need to move the gw-vm.qcow2 to a specific folder for each site that I download an image for.
I can't figure out how to cd into that randomly named folder to get at the gw-vm.qcow2 file.
Right now I am using os.system('unzip sitea.zip') to decompress.
I don't know how to cd into the resulting folder to then perform the following: os.system('mv gw-vm.qcow2 /opt/unetlab/addons/qemu/sc-branch-a-1.0/gw-vm.qcow2')
Any direction is appreciated.
It's going to be a function of os.walk() -- it performs a dirwalk on a directory. Check out the docs on it: https://docs.python.org/3/library/os.html?#os.walk
Good luck, hope that helps!
(edited formatting)

I want to add addtional files to existing archives (ZIP / RAR) or have the files added when compressing

I know how to do this for one archive at a time, but I want to add files, to multiple archives, in the same folder, simultaneously; if that is possible. I understand that I can do this with a batch file... but I don't know how to write the script / text.
So... I have several zip files in one folder. I want to add a specific text file and a specific image file to each/all of those zips. I don't want any other modifications of the zip files.
Or... is there a way to set WinRAR so that specific files will be automatically added whenever an archive is created?
Thanks
import zipfile
z = zipfile.ZipFile('cal.zip', mode='a', compression=zipfile.ZIP_DEFLATED)
z.write('/your/file/path') # or, z.writestr('your-filename', 'file-content')
z.close()

How to add the contents of a directory to a zip file?

How would I add the contents of an entire directory to an already existing zip file using python? The directory to be added to the zip file will also include other folders as well and there will be duplicates in the zip file that will need to be overwritten. Any help would be appreciated. Thanks in advance!
P.S. If it is possible to zip the directory then combine both files that would also work.
Python's zipfile module allows you to manipular ZIP compressed archives. The ZipFile.namelist() method returns a list of files in an archive, and the ZipFile.write() method lets you add files to the archive.
z = zipfile.ZipFile('myfile.zip')
The os.walk method allows you to iterate over all the files contained in a directory tree.
for root, dirs, files in os.walk('mydir'):
for filename in files:
z.write(os.path.join(root, filename))
Replacing a file in an archive appears to be tricky; you can removed items by creating a temporary archive and then replacing the original when you're done as described in this question.
It might be easier just to call the zip command instead, but put these together and you should be able to get to where you want.

Resources