Create Zip File and Ignore Directory Structure

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).

How to create a zip file without entire directory structure

I don't believe zip has a way to exclude the top level directory. I think your best bet would be to do something like:
pushd /foo; zip -r out.zip ./bar; popd;

But this is exactly the sort of answer you said you didn't want.

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)

zip file and avoid directory structure

The zipfile.write() method takes an optional arcname argument that specifies what the name of the file should be inside the zipfile

I think you need to do a modification for the destination, otherwise it will duplicate the directory. Use :arcname to avoid it. try like this:

import os
import zipfile

def zip(src, dst):
zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(src)
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
print 'zipping %s as %s' % (os.path.join(dirname, filename),
arcname)
zf.write(absname, arcname)
zf.close()

zip("src", "dst")

Can I maintain directory structure when zipping file?

@ErocM the link provided by @Flydog57 gives you exactly what you want. You are not exploiting the entryName argument correctly (the second argument in your case when calling CreateEntryFromFile).

Independently of which file you are adding to the archive (from same of different folders), you have to structure your archive using the entryName argument the C# api gives to you.

If your file's fullname is /tmp/myfile.txt, and you do archive.CreateEntryFromFile(item.FullName, item.Name), then the archive entry name will be myfile.txt. No folder created as the entry name doesn't contain folder structure in it's name.

However, if you call archive.CreateEntryFromFile(item.FullName, item.FullName), you will then have you file folder structure into the archive.

You can try with your function just changing item.Name into item.FullName.

Just be careful, on windows; if you path is C:\tmp\myfile.txt for instance, the archive will not be extractable correctly. You can then add some little code to remove C: from the full name of your files.

Some examples taking your implementation:

using System;
using System.IO;
using System.Collections.Generic;
using System.IO.Compression;

namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
FileInfo f1 = new FileInfo(@"/tmp/test1.txt");
FileInfo f2 = new FileInfo(@"/tmp/testdir/test2.txt");

List<FileInfo> files = new();
files.Add(f1);
files.Add(f2);

CreateZipFile(files, @"/tmp/archive.zip");

}

public static void CreateZipFile(IEnumerable<FileInfo> files, string archiveName)
{
using (var stream = File.OpenWrite(archiveName))
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create))
{
foreach (var item in files)
{
// Here for instance, I put all files in the input list in the same directory, without checking from where they are in the host file system.
archive.CreateEntryFromFile(item.FullName, $"mydir/{item.Name}", CompressionLevel.Optimal);

// Here, I am just using the actual full path of the file. Be careful on windows with the disk name prefix (C:, D:, etc...).
// archive.CreateEntryFromFile(item.FullName, item.FullName, CompressionLevel.Optimal);
}
}
}
}

Create zip folder without top level, but keep subfolders

The way to do this is:

cd bundle
zip -r ../bundle.zip *
cd ..

https://unix.stackexchange.com/a/182036/101265

Create a zip from folders retaining file structure including parent folders (and single files)

I tried this:

gulp.task('default', ()=>{
return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'], {base: '.'})
.pipe(zip('game.zip'))
.pipe(gulp.dest('deploy'))
})

Simply adding the {base: '.'} option to gulp.src does what you want. See gulp base option. Using {base: '.'} basically tells gulp to use all the directories in the dest location. Otherwise the default operation is to remove directories before the globs. So, in 'dist/**/*.*' the dist folder would not be retained without the base option.

I don't know where you get a game folder, I never do.



Related Topics



Leave a reply



Submit