How do I ZIP a file in C#, using no 3rd-party APIs?

How can I programatically (C#) ZIP a file (in Windows) without using any third party libraries? If using the 4.5+ Framework, there is now the ZipArchive and ZipFile classes. using (ZipArchive zip = ZipFile.Open(“test.zip”, ZipArchiveMode.Create)) { zip.CreateEntryFromFile(@”c:\something.txt”, “data/path/something.txt”); } You need to add references to: System.IO.Compression System.IO.Compression.FileSystem For .NET Core targeting net46, you need to … Read more

Unzipping files

I wrote an unzipper in Javascript. It works. It relies on Andy G.P. Na’s binary file reader and some RFC1951 inflate logic from notmasteryet. I added the ZipFile class. working example: http://cheeso.members.winisp.net/Unzip-Example.htm (dead link) The source: http://cheeso.members.winisp.net/srcview.aspx?dir=js-unzip (dead link) NB: the links are dead; I’ll find a new host soon. Included in the source is … Read more

Password protected zip file in java

Try the following code which is based on Zip4j: import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; import org.apache.commons.io.FilenameUtils; import java.io.File; public class Zipper { private String password; private static final String EXTENSION = “zip”; public Zipper(String password) { this.password = password; } public void pack(String filePath) throws ZipException { ZipParameters zipParameters = new ZipParameters(); … Read more

Read Content from Files which are inside Zip file

If you’re wondering how to get the file content from each ZipEntry it’s actually quite simple. Here’s a sample code: public static void main(String[] args) throws IOException { ZipFile zipFile = new ZipFile(“C:/test.zip”); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while(entries.hasMoreElements()){ ZipEntry entry = entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); } } Once you have the InputStream … Read more

Pairs from single list

My favorite way to do it: def pairwise(t): it = iter(t) return zip(it,it) # for “pairs” of any length def chunkwise(t, size=2): it = iter(t) return zip(*[it]*size) When you want to pair all elements you obviously might need a fillvalue: from itertools import izip_longest def blockwise(t, size=2, fillvalue=None): it = iter(t) return izip_longest(*[it]*size, fillvalue=fillvalue) With … Read more

Recommendations on a free library to be used for zipping files [closed]

You can try Zip4j, a pure java library to handle zip file. It supports encryption/ decryption of PKWare and AES encryption methods. Key features: Create, Add, Extract, Update, Remove files from a Zip file Read/Write password protected Zip files Supports AES 128/256 Encryption Supports Standard Zip Encryption Supports Zip64 format Supports Store (No Compression) and … Read more

How to create a zip file in Java

Look at this example: StringBuilder sb = new StringBuilder(); sb.append(“Test String”); File f = new File(“d:\\test.zip”); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f)); ZipEntry e = new ZipEntry(“mytext.txt”); out.putNextEntry(e); byte[] data = sb.toString().getBytes(); out.write(data, 0, data.length); out.closeEntry(); out.close(); This will create a zip in the root of D: named test.zip which will contain one single file … Read more

How are zlib, gzip and zip related? What do they have in common and how are they different?

Short form: .zip is an archive format using, usually, the Deflate compression method. The .gz gzip format is for single files, also using the Deflate compression method. Often gzip is used in combination with tar to make a compressed archive format, .tar.gz. The zlib library provides Deflate compression and decompression code for use by zip, … Read more

Appending files to a zip file with Java

In Java 7 we got Zip File System that allows adding and changing files in zip (jar, war) without manual repackaging. We can directly write to files inside zip files as in the following example. Map<String, String> env = new HashMap<>(); env.put(“create”, “true”); Path path = Paths.get(“test.zip”); URI uri = URI.create(“jar:” + path.toUri()); try (FileSystem … Read more