Rectangle 27 13

Passing a a ZipOutputStream to XSSFWorkbook.write will result in the stream being hijacked and closed by the workbook. This is because an XSSFWorkbook writes a .xlsx which is itself a zip archive of xml and other files (you can unzip any .xslx to see what's in there). If you're able to fit the excel file in memory, I've found this to work well:

ZipOutputStream zos = new ZipOutputStream(//destination outputstream);
zos.putNextEntry(new ZipEntry("AnExcelFile.xlsx"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
bos.writeTo(zos);
zos.closeEntry();
// Add other entries as needed
zos.close();
close
ByteArrayOutputStream
zos

java - write a XSSFWorkbook to a zip file - Stack Overflow

java apache-poi zipoutputstream
Rectangle 27 3

Okay. Thanks for posting. I didn't actually understand that from reading the CPAN documentation. You might consider making that point more clear in the next revision. Also, looks like there is a period at the end of one of your lines of code instead of a semicolon in your synopsis.

winapi - How do I archive a set of files using Perl on Win32 while ret...

perl winapi path archive tar
Rectangle 27 3

I have one proprietary library for making nice zip archive files out of memory data. It was designed to be multiplatform, but apparently not tested well enough on every platform including Windows.

It works great on Linux and other POSIX systems but as I tried to adopt it in my project, I've stubled upon this: How to suppress #define locally?

Both the library and winbase.h (included via the most standart windows.h) has a CreateFile entity. And, as in winbase it's just a macros, compiler don't see any problem, unless you actually try to use CreateFile in your code.

So yes, keeping your namespace clean might be a good idea.

OP is referring to literally standard libraries, which are specified in the ISO 14882 Standard for C++. To be called a C++ platform, the development system must include them. Things which are merely common such as windows.h are not standard headers. You mention an interaction between a platform library and your library, but the standard library isn't in play.

CreateFile is arguably a bad name for your library. You should prefix it with the library name, or say what type of file you're creating (e.g. CreateZipFile).

@Mehrdad If he puts it in his own namespace, it should be fine. Microsoft is insane to define a macro with such a name.

@Potatoswatter, Microsoft is crazy to define macros with the names min and max. I've always known about that, though, so it hasn't bitten me much. What did bite hard was their stupid near and far macros when I chose those names for parameters for near and far clipping plane distances. It compiled and I completely forgot about near and far pointers :( That was a constructor along the lines of Foo(float near, float far) : near_{near}, far_{far} {}. Of course being defined as nothing, that compiles, and it was hard to figure out even after narrowing down to that constructor.

@Potatoswatter: Oh I think I misunderstood the problem -- I was assuming his code was C, didn't realize he was talking about C++. Interesting, +1.

c++ - Why not include all the standard headers always? - Stack Overflo...

c++ coding-style
Rectangle 27 2

I am making decryption of password protected rar archives my next item to do as I thought I had already done it (zip files blurred my memory).

As another comment said, I am always looking for help but hopefully I'll get this done soon.

Was this implemented? If so can you show me how to do it? Many thanks :-)

C# NUnrar library (extracting password protected rar files without pas...

c# rar winrar
Rectangle 27 40

My suggestion would be to use a StringIO object. They emulate files, but reside in memory. So you could do something like this:

# get_zip_data() gets a zip archive containing 'foo.txt', reading 'hey, foo'

from StringIO import StringIO
zipdata = StringIO()
zipdata.write(get_zip_data())
myzipfile = zipfile.ZipFile(zipdata)
foofile = myzipfile.open('foo.txt')
print foofile.read()

# output: "hey, foo"
myzipfile = zipfile.Zipfile(StringIO(get_zip_data()))
for name in myzipfile.namelist():
    [ ... ]

"The StringIO object can accept either Unicode or 8-bit strings" Doesn't this mean that if the number of bytes you expect to write is not congruent to 0 mod 8, then you will either throw an exception or write incorrect data?

Small comment on the above code: when you read multiple files out of the .zip, make sure you read the data out one by one, because calling zipfile.open two times will remove the reference in the first.

Notice that as of Python 3 you have to use from io import StringIO

unzip - Python: downloading and unzipping a .zip file without writing ...

python unzip
Rectangle 27 40

My suggestion would be to use a StringIO object. They emulate files, but reside in memory. So you could do something like this:

# get_zip_data() gets a zip archive containing 'foo.txt', reading 'hey, foo'

from StringIO import StringIO
zipdata = StringIO()
zipdata.write(get_zip_data())
myzipfile = zipfile.ZipFile(zipdata)
foofile = myzipfile.open('foo.txt')
print foofile.read()

# output: "hey, foo"
myzipfile = zipfile.Zipfile(StringIO(get_zip_data()))
for name in myzipfile.namelist():
    [ ... ]

"The StringIO object can accept either Unicode or 8-bit strings" Doesn't this mean that if the number of bytes you expect to write is not congruent to 0 mod 8, then you will either throw an exception or write incorrect data?

Small comment on the above code: when you read multiple files out of the .zip, make sure you read the data out one by one, because calling zipfile.open two times will remove the reference in the first.

Notice that as of Python 3 you have to use from io import StringIO

unzip - Python: downloading and unzipping a .zip file without writing ...

python unzip
Rectangle 27 0

After browsing the SharpDevelop wiki I found the following which may be of assistance to you in helping you resolve the problem:

This should work, but you shoudl only use that for small files, as the size of the final zip is not availalbe, so the client browser can't show a progress. If the files are bigger create the zip in memory (MemoryStream) and then write the size and content of that to the response.

c# - Create zip from multiple files in memory - Stack Overflow

c# .net sharpziplib
Rectangle 27 0

using Ionic.Zip; // from NUGET-Package "DotNetZip"

public HttpResponseMessage Zipped()
{
    using (var zipFile = new ZipFile())
    {
        // add all files you need from disk, database or memory
        // zipFile.AddEntry(...);

        return ZipContentResult(zipFile);
    }
}

protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
    // inspired from http://stackoverflow.com/a/16171977/92756
    var pushStreamContent = new PushStreamContent((stream, content, context) =>
    {
        zipFile.Save(stream);
        stream.Close(); // After save we close the stream to signal that we are done writing.
    }, "application/zip");

    return new HttpResponseMessage(HttpStatusCode.OK) {Content = pushStreamContent};
}

The ZipContentResult method could also live in a base class and be used from any other action in any api controller.

that works great. Don't know why this went unnoticed

c# - Using ASP.NET Web API, how can a controller return a collection o...

c# .net asp.net-web-api dotnetzip
Rectangle 27 0

I have used zlib for .net in the past and there are also libraries that wrap the native zlib library to provide a managed solution. I needed to do something similar to what you are doing. I would do the compression directly in memory for smaller transfers and would zip to a file and then download the file from a url and unzip from file for much larger files.

.net - Easiest way to compress in Python and decompress with decompres...

c# .net python mono compression
Rectangle 27 0

For handling large files without loading them into memory, use the new stream-based methods in Python 2.6's version of zipfile, such as ZipFile.open. Don't use extract or extractall unless you have strongly sanitised the filenames in the ZIP.

(You used to have to read all the bytes into memory, or hack around it like zipstream; this is now obsolete.)

performance - What Is The Best Python Zip Module To Handle Large Files...

python performance compression zip extraction
Rectangle 27 0

Classes are loaded on demand. For normal JARs, very little is loaded eagerly (unless there are specific entries in the manifest, and I'm a bit rusty there, but most people don't use them). The JVM doesn't cache non-class resources in memory at all, so reading a resource from a JAR file is basically the same as manually reading an entry from a normal ZIP file. The only disadvantages I see are:

  • The resource is copied every time the JAR is copied, even if it doesn't change, and
  • The resource is read from a JAR file (which is compressed), even if it's something like an image which is already compressed.

I think ZIP files support per-file compression algorithms, and support a no-op compression algorithm, though, so the second point may not matter.

performance - Disadvantages to large resources in Java jar files? - St...

java performance jar embedded-resource
Rectangle 27 0

ftplib

The zipfile module allows extracting files from a zip file.

Here's the key, the io.BytesIO class allows you to pass in-memory bytes to anything that expects a file. (In Python 2.x, the StringIO module provides similar functionality.)

Download zip file via FTP and extract files in memory in Python - Stac...

python ftp zip
Rectangle 27 0

If you're using the zip extension, the answer seems to be "no." The close method in the ZipArchive class is what triggers a write, and it seems to want to write to a file. You might have some luck using Streams to simulate a file, but then you're keeping the entire thing in memory, and you still couldn't send it until you're done adding files.

If you're having problems with timeouts or other problems while having a user wait for the zip file to be created, try a multi-step download process:

  • User picks whatever they pick to create the zip
  • They're taken to a status page that uses ajax calls to actually build the zip file in the background. A pretty, distracting animation should keep their attention.
  • After the background processes build the zip, the user is redirected to a script that performs the download / redirected to the file.

Zip Stream in PHP - Stack Overflow

php zip stream
Rectangle 27 0

Create the package using a MemoryStream then.

zip - Creating a zipped Document that containing files from memory str...

c# zip bytearray memorystream
Rectangle 27 0

To the best of my knowledge, docx files are simply xml files in a ZIP container. To convert these to some XML format of your design, you would need to unzip the file (into new folder or into memory), load the target Xml document, and apply your XSLT to that xml file. I don't think you mention anything about your development environment, except the "docx4j" tag.. Are you developing in Java? If so, I'm afraid I wouldn't know what libraries to point you to for the zip-handling and xml-transformation libraries (although I know they exist, and it would only take a 5-minute google search to find them!)

To check out the xml files in a docx, simply change the extension of the file from ".docx" to ".zip" and open in your favorite ZIP archive tool.

xslt - Convert docX to a custom XML - Stack Overflow

xml xslt openxml docx docx4j
Rectangle 27 0

Use ZipEntry and PutNextEntry() for this. The following shows how to do it for a file, but for an in-memory object just use a MemoryStream

FileStream fZip = File.Create(compressedOutputFile);
ZipOutputStream zipOStream = new ZipOutputStream(fZip);
foreach (FileInfo fi in allfiles)
{
    ZipEntry entry = new ZipEntry((fi.Name));
    zipOStream.PutNextEntry(entry);
    FileStream fs = File.OpenRead(fi.FullName);
    try
    {
        byte[] transferBuffer[1024];
        do
        {
            bytesRead = fs.Read(transferBuffer, 0, transferBuffer.Length);
            zipOStream.Write(transferBuffer, 0, bytesRead);
        }
        while (bytesRead > 0);
    }
    finally
    {
        fs.Close();
    }
}
zipOStream.Finish();
zipOStream.Close();

Can you use 'using()' on the zipOStream instead of Close()?

If you're using .NET 4.5 there is a built in Zip class that is much easier to use than this abomination (SharpZipLib).

zipOStream.CloseEntry()
foreach

compression - Create Zip archive from multiple in memory files in C# -...

c# compression zip
Rectangle 27 0

Yes, you can use SharpZipLib to do this - when you need to supply a stream to write to, use a MemoryStream.

compression - Create Zip archive from multiple in memory files in C# -...

c# compression zip
Rectangle 27 0

using System.IO.Compression;


    ...

        byte [] pictureBuffer = null;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (var entry in archive.Entries)
            {
                if(entry.FullName == "SomePicture")
                {
                    using (zipEntryStream = entry.Open())
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        zipEntryStream.CopyTo(memStream);
                        pictureBuffer = zipEntryStream.ToArray;
                    }
                }
            }

I think this should work.(not tested) using the DotNetZip Library http://www.nuget.org/packages/DotNetZip/

using (ZipFile zip = new ZipFile(zipPath))
{
            foreach (var entry in zip.Entries)
            {
                if(entry.FileName == "SomePicture")
                {
                    using (var zipEntryStream = entry.OpenReader())
                    using (var memStream = new MemoryStream())
                    {
                        zipEntryStream.CopyTo(memStream);
                        pictureBuffer = zipEntryStream.ToArray;
                    }
                }
            }
}

vb.net - Extracting .zip files into memory at runtime (.NET) - Stack O...

vb.net
Rectangle 27 0

GZipStream does not support multiple files. Use SharpZipLib to compress multiple files.

c# - Add multiple in memory XmlDocument to a GZipStream or to a zip fi...

c# asp.net xml gzipstream
Rectangle 27 0

itertools.izip is perfect for this sort of thing since it does not require you to read the files into memory. It works just like zip except that it returns an iterator instead of a list of tuples. Something like the following should work.

import csv
import os
from itertools import izip, chain

def ajouter(files, out, path, repsim, delim=';'):

    try:
        open_files = [open(os.path.join(path, file_)) for file_ in files]
        readers = (csv.reader(f, delimiter=delim) for f in open_files)
        merged_cols = (tuple(chain.from_iterable(row)) for row in izip(*readers))


        with open(os.path.join(repsim, out), 'wb') as output:
            writer = csv.writer(output, delimiter=delim)
            writer.writerows(merged_cols)
    finally:
        for open_file in open_files:
            open_file.close()

it works but in the output file, column values are formated like this ['0.65']. Can you help me to modify this?

How to copy columns from several csv files into a single csv file with...

python csv