Zipping Without Creating Parent Folder

Zipping without creating parent folder

zip -r foo ./

assuming **

./

** i.e., present working directory is project in your case.

-r for recursively zipping

foo is the name of your zip file, i.e., foo.zip is the final zipped product you want.

zip files without including parent directories

According to the documentation, the R zip function's utilizes R_ZIPCMD, which is set in etc/Rcmd_environ. This is set to the command line zip windows utility by default. The R function provides the input parameter flags to pass additional input parameters to the underlying command line zip function. The manual which describes
flags can be downloaded here.

The -j flag allows just the file names to be stored rather than the full file path.

f_path <- 'C:\\path\\to\\dir\\out'
zip(f_path,
files = paste0(f_path, c('one.xlsx', 'two.xlsx')),
flags = '-r9Xj')

The -r9X portion of the flags input are the default parameters passed to underlying utility function and specify that the zip command should recursively search sub-directories, use maximum compression, and remove depreciated file fields.

This has only been tested using the windows zip utility. The necessary flag(s) may differ when using the unix utility.

Zipping files in a folder without a parent folder in the zipped file

The problem that is occuring here is that the $key in the iterator is the full path (since the RecursiveDirectoryIterator::KEY_AS_PATHNAME flag is used by default), which includes the relative part specified in the RecursiveDirectoryIterator constructor.

So for example we have the following hierarchy:

folderI
├─ folder1
│ ├─ item.php
│ └─ so.php
└─ folder2
└─ item.html

And an iterator created with new RecursiveDirectoryIterator("folderI").

When you echo out the $key you will get the following for item.php

folderI/folder1/item.php

Similarly, if we did new RecursiveDirectoryIterator("../somewhere/folderI"), then $key would look like:

../somewhere/folderI/folder1/item.php

You wish to get the path to the file, without including the path used to create the iterator. Thankfully, the RecursiveDirectoryIterator has a method whose sole purpose in life is to do exactly this.

RecursiveDirectoryIterator::getSubPathname()

You can use the following which will be easier

$newKey = $iterator->getSubPathname();

This will return the following for item.php, given the same folder structure shown above:

folder1/item.php

The method can be used like so:

foreach ($iterator as $key=>$value) {
$zip->addFile(realpath($key), $iterator->getSubPathname()) or die ("ERROR: Could not add file: $key");
}

Aside: we can use $iterator->getSubPathname() here because the RecursiveIteratorIterator passes along any calls to unknown methods to the "inner iterator", which in this case is the RecursiveDirectoryIterator. Equally, you could do $iterator->getInnerIterator()->getSubPathname(), both do the same job.

zipping files in python without folder structure

From the documentation:

ZipFile.write(filename, arcname=None, compress_type=None,
compresslevel=None)

Write the file named filename to the archive, giving it the archive
name arcname (by default, this will be the same as filename, but
without a drive letter and with leading path separators removed).

So, just specify an explicit arcname:

with zp(os.path.join(self.savePath, self.selectedIndex + ".zip"), "w") as zip:
for file in filesToZip:
zip.write(self.folderPath + file, arcname=file)

Create zip file and ignore directory structure

You can use -j.

-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).

zip the files and folders inside a parent directory without including the parent directory + Amazon Linux

Move into the folder, then zip :

cd my-folder
zip -r ../my-archive.zip *

Dirty, but effective

Or, in a more fancy way, using tar:

tar czf my-archive.tar.gz -C /path/to/my-foder/ ./

Zipfile with no parent folder

In your code you have a mistake in a word 'zipfiles': for file_extension in zipfile_list: (without 's' in the end).

It have to be for file_extension in zipfiles_list:.

Than I run your code in Win7x86 - it is OK, no inner folder (python 3.6.6).

Tried it on Win 10x64 - same.

Xubuntu 18.04 x64 - same, without folder.

Here is my script:

from os.path import basename 
from os import path
import os
import zipfile

# current directory
script_dir = path.dirname(path.abspath(__file__))

def create_zip(file_name, file_dir):
"""
Function to create zip archive
"""
os.chdir(file_dir)
zipfiles_list = ['csv', 'txt']
file_name_part = file_name
zip_name = '{0}.zip'.format(file_name_part)
with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zip_archive:
for file_extension in zipfiles_list:
full_file_path = '{0}.{1}'.format(file_name_part, file_extension)
zip_archive.write(full_file_path, basename(full_file_path))

create_zip("first", script_dir)

In a directory with script there two files: first.txt and first.csv. After runing this scrip I do have zip file first.zip with two files inside: first.txt and first.csv.



Related Topics



Leave a reply



Submit