Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need code to read text file from a zip file

Status
Not open for further replies.

sumanseshadri

Programmer
Dec 17, 2002
3
0
0
IN
Hi,
I need java code to read a text file from a zip file. The zip file contains only 1 text file.And I need to print the contents of the text file in on the console.I need the output of the text file as a string

Any help is highly appreciated...

thanx
suman
 
Look at the java.util.zip package ...
 
Hi Sedj,
Thanx. I did look at the api.


I have the following code , but this prints only few lines of the text file.
Could you please check ..Here, ivwr is a zip file.

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream("D:\\BuildEngine\\bin\\Test.ivwr")));

for (; zis.available() > 0; )
{
ZipEntry nextEntry = zis.getNextEntry();
if (nextEntry == null)
{
break;
}
long compressedSize = nextEntry.getCompressedSize();
byte[] unzippedByteArray = new byte[(int)nextEntry.getSize()];
zis.read(unzippedByteArray, 0, (int)compressedSize);


CharArrayWriter writer = new CharArrayWriter();
for(int i1 = 0; i1 < unzippedByteArray.length; i1++)
{
writer.write(unzippedByteArray[i1]);
// System.out.println(&quot;SS:&quot;+unzippedByteArray[i1]);
}


//This prints only few lines ..dunno why..
System.out.println(&quot;AAAAAA:&quot;+ writer.toString());

}

 
Code:
                    CharArrayWriter writer = new CharArrayWriter();
                    for(int i1 = 0; i1 < unzippedByteArray.length; i1++)
                    {
                        writer.write(unzippedByteArray[i1]);
                    //    System.out.println(&quot;SS:&quot;+unzippedByteArray[i1]);
                    }


//This prints only few lines ..dunno why..
                    System.out.println(&quot;AAAAAA:&quot;+ writer.toString());

this should be in a while loop testing to see if if there is more to read. that's why you only get the first few lines. that's all that comes through on the first buffer.

MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
better...

Code:
// Written by W.Buchanan, 2003
import java.io.*;
import java.util.zip.*;

public class unzip {
public static void main (String argv[]) {
try {
BufferedOutputStream destination = null;
FileInputStream finps = new FileInputStream(argv[0]);
ZipInputStream zipinp = new ZipInputStream(new BufferedInputStream(finps));
ZipEntry entry;
while((entry = zipinp.getNextEntry()) != null) {
System.out.println(&quot;UnZipping: &quot; + entry);
int count;
byte databuff[] = new byte[2048];
// write the files 
FileOutputStream fos = new FileOutputStream(entry.getName());
destination = new BufferedOutputStream(fos, 2048);
while ((count = zipinp.read(databuff, 0, 2048)) != -1) {
destination.write(databuff, 0, count);
}
destination.flush();
destination.close();
}
zipinp.close();
} catch(Exception e) {
}
}
}

MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top