10. januar 2002 - 08:51
#1
ja det kan man godt
Nedenstående eksempel tilføjer en fil til et eksisterende zip arkiv.
Det sker ved at indholdet af en eksisterende kopieres over i et nyt arkiv og ny data tilføjes.
import java.util.*;
public class WernerZip extends java.lang.Object
{
static java.util.zip.ZipOutputStream outZip;
/** Creates new Zip */
public WernerZip()
{
}
public WernerZip(String[] args)
{
if(args.length <3) //we need at least 3 arguments
{
System.out.println(\"Syntax: java Zip <Standard zipfile> <Output zipfile> <fileToAdd 1 or more> \");
System.exit(1); //wrong number of arguments
}
String zipName=args[0]; //get zip filname from arguments
String outName=args[1]; //get output zip filname from arguments
File inputFile=new File(zipName);
if(!inputFile.exists()) //check if input zip file exists
{
System.out.println(\"Error 2\");
System.exit(2);
}
//check if to add files exists
for(int x=2;x<args.length;x++) //loop through all files to add to see if the exists
{
inputFile=new File(args[x]);
if(!inputFile.exists()) //check one of them
{
System.out.println(\"Error 3\");
System.exit(3);
}
}
try
{
outZip=new java.util.zip.ZipOutputStream(new FileOutputStream(outName)); //create the new zip file
copyZip(zipName,outName); //copy contens of input zip file to the new zip file
for(int x=2;x<args.length;x++) //loop through files to add
{
addFile(outZip,args[x]); //add a new file to the zip file
}
outZip.close(); //done
}
catch (IOException e)
{
e.printStackTrace(System.out); //deep trouble
}
}
/**
* @param args the command line arguments
*/
public static void main (String args[])
{
WernerZip zip=new WernerZip(args);
}
private void copyZip(String inName, String outName) throws IOException
{
java.util.zip.ZipFile inZip = new java.util.zip.ZipFile(inName); //open input zip file
Enumeration enum = inZip.entries(); //get enumerator in order to iterate through files in input zip file
while(enum.hasMoreElements()) //iterate
{
java.util.zip.ZipEntry entry = (java.util.zip.ZipEntry) enum.nextElement(); //get zip entry for a single file
InputStream in=inZip.getInputStream(entry); //get the input stream so we can read the file
byte data[]=new byte[16384]; //read 16k at a time
int len=0;
java.util.zip.ZipEntry entryOut = new java.util.zip.ZipEntry(entry.getName()); //create zip entry in output zip file
outZip.putNextEntry(entryOut); //insert entry in output zip file
while(len!=-1) //EOF ?
{
len=in.read(data,0,16384); //read max 16 KB of data
if(len!=-1) //anything read from file ?
{
outZip.write(data,0,len); //add the block to output zip file
}
}
outZip.closeEntry(); //done
in.close(); //done
}
}
private void addFile(java.util.zip.ZipOutputStream z, String fileName) throws IOException
{
String zipFileName=fileName; //copy filename
int pos=zipFileName.lastIndexOf(\'\\\\\'); //find last index of the \'\\\' char
if(pos!=-1) //was there a \'\\\'
{
zipFileName=zipFileName.substring(pos+1); //remove everything before and including the last \'\\\'
}
java.util.zip.ZipEntry entry = new java.util.zip.ZipEntry(zipFileName); //create a new zip entry and replace \\ with /
//her skal filen addes
z.putNextEntry( entry ); //add entry
FileInputStream in = new FileInputStream(fileName); //open input file
byte data[]=new byte[16384]; //læs 1k af gangen
int len=0;
while(len!=-1) //anything read from file
{
len=in.read(data,0,16384); //read 16Kb max
if(len!=-1)
{
z.write(data,0,len); //add the data to the zip file
}
}
in.close(); //Close the file
z.closeEntry();
}
}
10. januar 2002 - 11:01
#2
disky:
er der en grund til linien
static java.util.zip.ZipOutputStream outZip;
Er det ikke lidt farligt at lade en sådan variabel
være statisk? Det vil have minimale omkostninger
at lave den til en instansvariabel og gøre klassen
mere sikker.
10. januar 2002 - 11:13
#3
Hmm, det har du forresten ret i.
Jeg kan egentligt ikke huske hvorfor den er static, jeg tror det er en rest fra da vi kørte det hele direkte fra main()
p.s. der er LANG tid siden den blev lavet.
Jeg har allerede rettet det i min version :)
Jeg takker