Create compress file with password?

hi I just want to know if there is a way to compress a file with a password, or if there is an example that can help me I searched but did not find anything interesting with which I'm comfortable with, because I don't want to protect with an encryption key I want a password that I can put for example in a variable and use it afterwards

let pass: &'static str = "password";


Do you want to use a specific compression format? There are a lot of different formats for storing multiple and/or compressing files of which some support encryption, I guess.


.zip or .rar just a simple example compress a file or a folder for example and the result for example with my password password if I want to decompress it after I have to enter my password

I found a zip crate, but it only supports decryption, and no encryption :confused:. Depending on how interoperable you want/need to be, you could split up the task: use one crate to create an archive, and another crate to encrypt the data.

Sadly I didn't see a high-level library for this either, or at least, not a widely used one.

On the topic more generally -- I guess there's some secure extensions for secure .zip file encryption now (albeit apparently with licensing issues), but historically zip encryption has been a joke (like, crack it in DOS level). Take care to look into this if you find a zip library; .rar might be the better bet. I'm personally used to treating the two concerns (compression, encryption) separately at the file level, e.g. PGP (gpg) on top of gzip or whatever, but didn't see any great options there either.

Will any of these work?

(I'm a big fan of 7-Zip. It has served me well over a long stretch of time.)

(Looks like some rough seas in that direction but this at least has a high probability of working.)

Thank jbe you it's a good idea I already used the "0.5.5" and the "0.6.2" both are good

i have an example i will share it i am working on it

I also found this: serde-encrypt, but it doesn't come with a password hashing algorithm, I think (didn't really check the crate exhaustingly).

Here a quick search engine result: Rust cryptography libraries: A comprehensive list - LogRocket Blog

Thank you quinédot it's a good idea too but nothing and impossible of the computer language

simple compresse file maybe it helps someone

use std::fs;
use std::fs::File;
use std::io::{copy, Read, Seek, Write};
use std::path::Path;
use std::str;
use walkdir::{DirEntry, WalkDir};
use zip::write::FileOptions;

fn main() 
compress_file(Path::new("C:/file.txt"), Path::new("C:/"));

fn compress_file(src_dir: &Path, target: &Path) 
    let zipfile = std::fs::File::create(target).unwrap();
    let dir = WalkDir::new(src_dir);
    let prefix = src_dir.parent().map_or_else(||"/",|p|p.to_str().unwrap());
    zip_dir(&mut dir.into_iter().filter_map(|e| e.ok()), prefix, zipfile);

fn zip_dir<T>(it: &mut dyn Iterator<Item = DirEntry>, prefix: &str, writer: T) -> zip::result::ZipResult<()>
    where T: Write + Seek 
    let mut zip = zip::ZipWriter::new(writer);
    let options = FileOptions::default()

    let mut buffer = Vec::new();
    for entry in it {
        let path = entry.path();
        println!("prefix  {:?} ...", prefix);
        let name = path.strip_prefix(Path::new(prefix)).unwrap();
        println!("name  {:?} ...", name);
        if path.is_file() {
            println!("adding file {:?} as {:?} ...", path, name);
            zip.start_file_from_path(name, options)?;
            let mut f = File::open(path)?;

            f.read_to_end(&mut buffer)?;
        } else if name.as_os_str().len() != 0 {
            println!("adding dir {:?} as {:?} ...", path, name);
            zip.add_directory_from_path(name, options)?;

Thanks Codage mal I'm going to try

Thanks interesting I will see

can you give us an example how to create and put a password or a key with 7-zip it makes me very happy maybe I will change my decision and I will ver 7-zip

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.