Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for _, filename := range list {
input, err := os.Open(filename)
if err != nil {
return err
}
output, err := w.Create(filename)
if err != nil {
return err
}
_, err = io.Copy(output, input)
if err != nil {
return err
}
}
err := w.Close()
if err != nil {
return err
}
err = os.WriteFile(name, buf.Bytes(), 0777)
if err != nil {
return err
}
list contains filenames of files existing in the filesystem.
In this example, the zip data is buffered in memory before writing to the filesystem.
In this example, the zip data is buffered in memory before writing to the filesystem.
Zip := TZipper.Create;
Zip.FileName := name;
Zip.Entries.AddFileEntries(list);
Zip.ZipAllFiles;
Zip.Free;
my @list = ( 'file_A.txt', 'file_B.txt' );
zip \@list => 'name.zip'
or die "zip failed: $ZipError\n";
Core module IO::Compress::Zip (available since v5.9.4) provides a zip function that takes a reference to a list (i.e. \@list not @list) and the name of the zip file. If it fails, $ZipError contains the error message. See documentation for details.
let path = std::path::Path::new(_name);
let file = std::fs::File::create(&path).unwrap();
let mut zip = zip::ZipWriter::new(file); zip.start_file("readme.txt", FileOptions::default())?;
zip.write_all(b"Hello, World!\n")?;
zip.finish()?;
zip.finish() returns a Result.
fn zip(_name: &str, _list: Vec<&str>) -> zip::result::ZipResult<()>
{
let path = std::path::Path::new(_name);
let file = std::fs::File::create(&path).unwrap();
let mut zip = zip::ZipWriter::new(file);
for i in _list.iter() {
zip.start_file(i as &str, FileOptions::default())?;
}
zip.finish()?;
Ok(())
}
Note: This function does not write any contents to the files.