using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace CsCreateZip { class Program { [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern void LoadLibrary(String lpFileName); static void Main(string[] args) { if (Environment.Version.Major >= 4) { String folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"C:\Windows\Microsoft.NET\Framework\v2.0.50727"); folder = Path.GetFullPath(folder); LoadLibrary(Path.Combine(folder, "vjsnativ.dll")); } //作成するZIPファイルの設定 string zipPath = "C:\\test\\test2.zip"; //圧縮するファイルの設定 string[] filePaths = { "C:\\test\\1.bmp", "C:\\test\\2.bmp" }; //ZipOutputStreamの作成 java.io.FileOutputStream fos = new java.io.FileOutputStream(zipPath); java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos); //Zipにファイルを追加する foreach (string file in filePaths) { //ZIPに追加するときのファイル名を決定する string f = System.IO.Path.GetFileName(file); //ディレクトリを保持する時は次のようにする //string f = file.Remove( // 0, System.IO.Path.GetPathRoot(file).Length); //f = f.Replace("\\","/"); java.util.zip.ZipEntry ze = new java.util.zip.ZipEntry(f); ze.setMethod(java.util.zip.ZipEntry.DEFLATED); zos.putNextEntry(ze); //FileInputStreamの作成 java.io.FileInputStream fis = new java.io.FileInputStream(file); //書込み sbyte[] buffer = new sbyte[8192]; int len; while ((len = fis.read(buffer, 0, buffer.Length)) > 0) { zos.write(buffer, 0, len); } //閉じる fis.close(); zos.closeEntry(); } zos.close(); fos.close(); } } }