//////////////////// Main.java ////////////////////
class Main
{
public static void main(String args[])
{
new ZipDeflator(args);
}
}
////////////////////////////////////////////////////////////
//////////////////// ZipDeflator.java ////////////////////
import java.util.zip.*;
import java.io.*;
class ZipDeflator
{
public ZipDeflator(String files[])
{
try
{
String archive_name = files[0];
ZipOutputStream output = new ZipOutputStream(new FileOutputStream(archive_name));
output.setMethod(ZipOutputStream.DEFLATED);
for(int i=1; i<files.length; i++)
{
byte[] data = getDataFromFile(files[i]);
ZipEntry zip_entry = new ZipEntry(files[i]);
System.out.println("File: " + files[i]);
System.out.print("Deflating...");
output.putNextEntry(zip_entry);
output.write(data, 0, data.length);
output.closeEntry();
System.out.println("Done!");
System.out.println("");
}
output.close();
}
catch(IOException e)
{
System.err.println(e);
System.exit(1);
}
}
// ファイル中のデータをbyte配列にして返す
public byte[] getDataFromFile(String file_name)
{
try
{
File file = new File(file_name);
int file_length = (int)file.length();
FileInputStream file_in = new FileInputStream(file);
byte data[] = new byte[file_length];
int read_bytes, offset = 0;
while((read_bytes = file_in.read(data, offset, file_length)) != -1)
{
offset += read_bytes;
if(offset == file_length)
break;
}
file_in.close();
return(data);
}
catch(IOException io_ex)
{
System.err.println(io_ex);
return(null);
}
catch(Exception e)
{
System.err.println(e);
return(null);
}
}
}
////////////////////////////////////////////////////////////
Source is here. (ZIP Format, 931Byte, Shift-JIS)