|
hi..every one..
i red many code from this site...it's really helpful 4 me...but i want to know how can i read files from a zip file and display it's file contents in IE...any one help for this...
|
|
|
|
|
Hi,
I am trying following code to zip file using Java,
import java.io.*;
import java.util.zip.*;
public class Zip3 {
public static void zipIt(String p, String f){
byte[] buf = new byte[1024];
try {
// Create the ZIP file
String f2 = f.substring(0,f.length()-4);
String outFilename = f2+".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
String[] filenames = new String[]{p+f};
// Compress the files
for (int i=0; i<filenames.length; i++)="" {
="" fileinputstream="" in="new" fileinputstream(filenames[i]);
=""
="" add="" zip="" entry="" to="" output="" stream.
="" out.putnextentry(new="" zipentry(filenames[i]));
="" transfer="" bytes="" from="" the="" file="" file
="" int="" len;
="" while="" ((len="in.read(buf))"> 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (IOException e) {
}
}
public static void main(String args[])throws IOException {
Zip3 zipper = new Zip3();
zipper.zipIt("E:\\Data\\ExcelFiles\\","File.xls");
}
}
1. I am unable to find out way to add destination folder for zipped file.
2. Another problem i am facing is the zip file created resides inside "E:\Data\ExcelFiles\" inside zipped file.
Can anybody help me?
Thanks,
Sanjit
|
|
|
|
|
1.. I am unable to find out way to add destination folder for zipped file.
ans) java.io.FileOutputStream fos = new java.io.FileOutputStream("zipfolder/filename.zip"); simply in this way you can specify destination folder if u r using ASP.net u can use Server.mappath() like this
java.io.FileOutputStream fos = new java.io.FileOutputStream(Server.MapPath("~/zipfolder/basket.zip"));
2. Another problem i am facing is the zip file created resides inside "E:\Data\ExcelFiles\" inside zipped file.
ans) java.util.zip.ZipEntry ze = new java.util.zip.ZipEntry(sourceFile.Substring(20).Replace('\\','/'));
here substring(20) is area that can solve your 2nd problem set the parameters according to your folder path
|
|
|
|
|
I've been looking for a dependable Zip solution for .NET (C#) and I haven't found one. One thing I did notice during my research is that .NET has some "package" classes, but its funny how no one here talks about these classes.
The classes of interest are "Package" and more interestingly "ZipPackage".
Has anyone looked at these classes? I have, but I haven't found good examples on them. Are these classes not what I think they are? If they are Zip Compression classes why hasn't there been any mention of them here in Code Project?

|
|
|
|
|
|
I use this code in VB
For Each sourceFile In astrFileNames
Dim fis As java.io.FileInputStream = New java.io.FileInputStream(sourceFile)
' File name format in zip file is:
' folder/subfolder/filename
' Let's delete drive name and replace '\' with '/':
Dim ze As java.util.zip.ZipEntry = New java.util.zip.ZipEntry(sourceFile.Substring(3).Replace("\\", "/"))
zos.putNextEntry(ze)
Dim buffer(1024) As System.SByte
Dim len As Integer
While ((len = fis.read(buffer)) >= 0)
zos.write(buffer, 0, len)
End While
zos.closeEntry()
fis.close()
Next
zos.close()
fos.close()
This code is the C# translate but didn't work fine . Specialy when there is while structure fis.read instruction didn't read a byte and len all time is 0.
Some one ca help me?
Regards
Ale
|
|
|
|
|
Is it ok to use this code with text files ?
|
|
|
|
|
Hello
As vjslib is buggy (don't use it) try this one:
http://www.icsharpcode.net/OpenSource/SharpZipLib/
There are 3 advantages over vjslib:
1.) It works without bugs.
2.) It also supports GZip, Tar and BZip2 .
3.) The DLL you need is only 140 kB instead of 3,7 MB.
The SharpZipLib has a good detailed documentation but it lacks examples.
So I post my code here which I use to zip a whole folder into a ZIP file.
Elmü
using ICSharpCode.SharpZipLib.Zip;
static public void PackFolderIntoZipFile(string s_Folder, string s_ZipFile)
{
ZipOutputStream i_zStream = new ZipOutputStream(File.Create(s_ZipFile));
i_zStream.SetLevel(5);
int s32_BaseFolderLen = s_Folder.Length;
if (s_Folder.Substring(s_Folder.Length-1) != "\\") s32_BaseFolderLen ++;
ArrayList i_FileList = new ArrayList();
EnumFiles(ref i_FileList, s_Folder, "*.*", true);
Byte[] u8_Buf = new Byte[0x100000];
foreach (string s_File in i_FileList)
{
FileStream i_fStream = null;
try { i_fStream = File.OpenRead(s_File); }
catch { continue; }
string s_ZipPath = s_File.Substring(s32_BaseFolderLen).Replace("\\", "/");
ZipEntry i_ZipEntr = new ZipEntry(s_ZipPath);
FileInfo i_Info = new FileInfo(s_File);
i_ZipEntr.DateTime = i_Info.LastWriteTime;
i_zStream.PutNextEntry(i_ZipEntr);
while (true)
{
int s32_Len = i_fStream.Read(u8_Buf, 0, u8_Buf.Length);
if (s32_Len <= 0)
break;
i_zStream.Write(u8_Buf, 0, s32_Len);
}
i_fStream.Close();
}
i_zStream.Finish();
i_zStream.Close();
}
static public void EnumFiles(ref ArrayList i_FileList, string s_Path, string s_Filter, bool b_Subfolders)
{
if (s_Path == null || s_Path == "")
return;
string[] s_Files = Directory.GetFiles(s_Path, s_Filter.Trim());
foreach (string s_File in s_Files)
{
i_FileList.Add(s_File);
}
if (b_Subfolders)
{
string[] s_Dirs = Directory.GetDirectories(s_Path);
foreach (string s_Dir in s_Dirs)
{
EnumFiles(ref i_FileList, s_Dir, s_Filter, true);
}
}
}
|
|
|
|
|
|
Another problem
When using the dll from http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx, there is
another big problem - Encoding.
The problem is that this dll has lots of problems when the files or folders contains unicode charcters
(Chinese, hebrew and so on).
You can read about it in the forum at: http://www.icsharpcode.net/OpenSource/SD/forum/forum.asp?
FORUM_ID=16&CAT_ID=8&Forum_Title=SharpZipLib
You can use the search and look for "encoding".
Does anyone knows how to solve this problem ?
|
|
|
|
|
To me it looks like an error in "ZipFile zf = new ZipFile("test.zip");"
Your example code fails to open the test.zip it created itself, but tools for zip-checking nonetheless found test.zip healthy and ok.
And if you use ZipInputStream there is no problem at all, so I suppose the bug is in the opening test.zip with "new ZipFile(...)".
Why do you think test.zip is corrupt? You have any (un)zip-program rating it as corrupt?
Sam
|
|
|
|
|
Hi Sam
While WinZip opens it fine, if you try to delete one of the files inside the zip from within WinZip, WinZip will delete the file and then bring up the “errors were found” dialog box where it reports a corrupt header. This only happens on occasions – so the bug is probably not easy to track.
Also, if you read the Google links I posted in one of the below threads, you’ll see a lot of people mentioning corrupted date/time entries too.
Nish
Now with my own blog - void Nish(char* szBlog);
My MVP tips, tricks and essays web site - www.voidnish.com
Request - Could everyone who have in the past, posted on my personal forum on CP, be kind enough to delete all your posts please? I intend to start a personal non-technical blog there, now that it is RSSd and would very much like to empty the forum before I do so - because the posts that are there as of now are mostly test posts and posts that were made before we all knew it was a blog
|
|
|
|
|
But if the zip really is corrupt there sure must be some tool to show that it is damaged and what exactly is corrupt, not?
|
|
|
|
|
SamJost wrote:
But if the zip really is corrupt there sure must be some tool to show that it is damaged and what exactly is corrupt, not?
The zip data is not corrupted - but the zip headers are. So no data is actualyl lost, but a program that expects proper headers will not be able to read the data back correctly.
Here is a post from a J# team member :-
From: Shankar Adhi [MSFT] (shankara.vjcr@online.microsoft.com)<br />
Subject: Re: java.util.zip problem <br />
Newsgroups: microsoft.public.dotnet.vjsharp<br />
Date: 2003-05-09 06:40:56 PST <br />
<br />
<br />
This is a known issue in J# class library. It will be fixed possibly in the next release of J#. <br />
<br />
Work Around: Decompress your zip file and again compress it using winzip, the new zip file should work. Thanks for using J#.<br />
<br />
ShankarA
Now with my own blog - void Nish(char* szBlog);
My MVP tips, tricks and essays web site - www.voidnish.com
Request - Could everyone who have in the past, posted on my personal forum on CP, be kind enough to delete all your posts please? I intend to start a personal non-technical blog there, now that it is RSSd and would very much like to empty the forum before I do so - because the posts that are there as of now are mostly test posts and posts that were made before we all knew it was a blog
|
|
|
|
|
Yes, I do not object to that, but still there must be some tool to show what exactly is wrong with the zip headers, or not?
|
|
|
|
|
try
{
java.io.FileInputStream fis = new java.io.FileInputStream("test.zip");
java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(fis);
java.util.zip.ZipEntry ze;
sbyte[] buf = new sbyte[1024];
int len;
string fileName;
while ((ze = zis.getNextEntry()) != null)
{
fileName = ze.getName();
java.io.FileOutputStream fos = new java.io.FileOutputStream(fileName);
while ((len = zis.read(buf)) >= 0)
{
fos.write(buf, 0, len);
}
fos.close();
}
zis.close();
fis.close();
MessageBox.Show("Everything looks fine!");
}
catch
{
MessageBox.Show("Bug confirmed!");
}
|
|
|
|
|
See these two links for more info :-
Nish
Now with my own blog - void Nish(char* szBlog);
My MVP tips, tricks and essays web site - www.voidnish.com
Request - Could everyone who have in the past, posted on my personal forum on CP, be kind enough to delete all your posts please? I intend to start a personal non-technical blog there, now that it is RSSd and would very much like to empty the forum before I do so - because the posts that are there as of now are mostly test posts and posts that were made before we all knew it was a blog
|
|
|
|
|
Ok! The problem is in reading over from the file inside the ZipFile class If u create a FileInputStream and u pass it to ZipInputStream it works fine.
This code solve:
using System;
using System.IO;
using java.util.zip;
class MainClass
{
[STAThread]
static void Main(string[] args)
{
string zipFileName = "test.zip";
string sourceFileName = "div.exe";
Zip(zipFileName,sourceFileName);
UnZip(zipFileName);
}
static void Zip(string zipFileName,string sourceFileName)
{
ZipOutputStream os = new ZipOutputStream(new java.io.FileOutputStream(zipFileName));
ZipEntry ze = new ZipEntry("div.exe");
ze.setMethod(ZipEntry.DEFLATED);
os.putNextEntry(ze);
java.io.FileInputStream fs = new java.io.FileInputStream(sourceFileName);
sbyte[] buff = new sbyte[1024];
int n = 0;
while ((n=fs.read(buff,0,buff.Length))>0)
{
os.write(buff,0,n);
}
fs.close();
os.closeEntry();
os.close();
Console.WriteLine("Compression ok!");
}
static void UnZip(string zipFileName)
{
try
{
java.io.FileInputStream fis = new java.io.FileInputStream(zipFileName);
java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(fis);
java.util.zip.ZipEntry ze2;
sbyte[] buf = new sbyte[1024];
int len;
string fileName;
while ((ze2 = zis.getNextEntry()) != null)
{
fileName = ze2.getName();
java.io.FileOutputStream fos = new java.io.FileOutputStream(fileName);
while ((len = zis.read(buf)) >= 0)
{
fos.write(buf, 0, len);
}
fos.close();
}
zis.close();
fis.close();
Console.WriteLine("Decompression ok!");
}
catch
{
Console.WriteLine("Problem in decompression!");
}
}
}
|
|
|
|
|
I'm using this code in my demo program: http://www.codeproject.com/csharp/VmEasyZipUnZip.asp. It works fine, I know.
Problem is in corrupted header.
Valeri
|
|
|
|
|
I tried using bug demo code, exactly as you have given (ok, not exactly. I used button click event in windows app instead of console app). But, it works fine for me. Am I missing something.
//Start of joke
Never comment ur code. If it was hard to write, it should be hard to understand !!!
//End of joke
|
|
|
|
|
Try with a binary file greater than 120 Kb or so.
Nish
Now with my own blog - void Nish(char* szBlog);
My MVP tips, tricks and essays web site - www.voidnish.com
Request - Could everyone who have in the past, posted on my personal forum on CP, be kind enough to delete all your posts please? I intend to start a personal non-technical blog there, now that it is RSSd and would very much like to empty the forum before I do so - because the posts that are there as of now are mostly test posts and posts that were made before we all knew it was a blog
|
|
|
|
|
Sorry, my file was less than 120 kb.
//Start of joke
Never comment ur code. If it was hard to write, it should be hard to understand !!!
//End of joke
|
|
|
|