How to zip an array in memory?

I have a byte array, and I intend to zip it into another array. How can I do it without having to create a temporary ZIP file to store the zipped contents?
Here is the small prototype code have I developed to try the idea (ZipTest.java):
import java.util.zip.*;
import java.io.*;
public class ZipTest{
     static public void main(String[] args)
      throws IOException{
          //testing stream without zipping first
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          byte[] b = {1, 2, 3, 4, 5};
          stream.write(b, 0, b.length);
          byte[] b2 = stream.toByteArray();
          for(int i = 0; i <= b2.length - 1; i += 1)
               System.out.println(b2[i] + "");
          //the previous code piece works well and generates the proper output
          //now, testing stream with zipping
          stream = new ByteArrayOutputStream();
          ZipOutputStream zip = new ZipOutputStream(stream);
          zip.write(b, 0, b.length); // the exception is generated here
          b2 = stream.toByteArray();
          for(int i = 0; i <= b2.length - 1; i += 1)
               System.out.println(b2[i] + "");
}The output is the following:
1
2
3
4
5
Exception in thread "main" java.util.zip.ZipException: no current ZIP entry
        at java.util.zip.ZipOutputStream.write(Unknown Source)
        at ZipTest.main(ZipTest.java:19)What should I do to solve this problem?

Try
public class ZipTest {
    public static void main(String[] args) throws IOException {
        //testing stream without zipping first
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        byte[] b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        stream.write(b);
        stream.close();
        byte[] b2 = stream.toByteArray();
        System.out.println("Uncompressed len=" + b2.length + ' ' + Arrays.toString(b2));
        //the previous code piece works well and generates the proper output
        //now, testing stream with zipping
        ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
        DeflaterOutputStream zip = new DeflaterOutputStream(stream2);
        zip.write(b);
        zip.close();
        byte[] b3 = stream2.toByteArray();
        System.out.println("Compressed len=" + b3.length + ' ' + Arrays.toString(b3));
}You will note that for trivial messages the header adds more than the compression saves.

Similar Messages

  • Large Arrays and Memory

    I'm supposed to be working on code for a lab, and they have reported possible problems with labVIEW eating through memory on long experiments.  Someone before me tried to fix the problem but I am unsure if it is actually helping.  (I'm more familiar with languages like C++, and have not used labVIEW prior to this summer). 
    Where I believe the problem lies is with the array (within a loop).  Depending on the experiment the arrays will be of different sizes so how they handle the array is:
    -> It is an array of a cluster of 2 elements
    -> The array is wired to a shift register.
    -> The shift register is initialized prior to the loop opening by wiring the shift register to a cluster of 2 "0's".
    ->Each loop cycle they add new data (a new cluster) to the array using "Build Array"
    There are multiple of these arrays all being plotted so they use "Build Cluster Array" and then wire it to the corresponding Plot (an XY Graph).  They use this after "Build Array".
    This used to be it, so the arrays would grow large and crash the program.  Someone before me added an option to clear the arrays, but I am unsure if the way she designed it actually releases the memory since they are still reporting some problems.  The user enters a number in a control "Clear After:".  On every iteration that is a multiple of that number, the program passes the shift register an array with one element.  The array that is passed set up the same as the array passed for the initialization process. 
    My concern is that the code never specifically says delete the array or release the memory.  It feels very similar to the situation in C++ when the programmer dynamically creates an array (using new) but never deallocates the array (using delete), instead they just change where the pointer is pointing.  There the memory would still be tied up and unusable. 
    So I guess my question is, looking at the process above do I need use "Delete from Array" to release the memory and allow the program to run faster on longer experiments with large datasets or does labVIEW automatically deallocate that memory and therefore I should I be looking elsewhere in my program for processes that would slow down everything on longer experiments?
    Thanks,
    Val
    Solved!
    Go to Solution.

    I have attached a photo of the portion of code that I was referring to.  It shows 2 photos so you can see all possibilities in the 2 case statements.
    The first picture is when the cycle is adding new data points, and does not clear the array.
    The second picture shows the program passing through the array (which it does every second cycle) and then "clearing" the array.  (Which as I state above, I didn't know if that was correct).
    (None of this is actually my code, I was hired on to upgrade them from labVIEW 5.1 to labVIEW 2009.  They just asked me to look at this.  It seems to work fine on smaller length experiments on the order of a couple of hours).  If you need anything else from me, don't hesitate to ask.
    Thanks,
    Val
    Attachments:
    loop.docx ‏105 KB

  • Does anyone know how to display (in LabVIEW) the memory use during execution of an image and data acquisitio​n VI to predict when it is time to cease the acquisitio​n to prevent the program crashing?

    Does anyone know how to display (in LabVIEW) the memory use during execution of an image and data acquisition VI to predict when it is time to cease the acquisition to prevent the program crashing?
    I am acquiring images and data to a buffer on the edge of the while loop, and am finding that the crashing of the program is unpredictable, but almost always due to a memory saturation when the buffers gets too big.
    I have attached the VI.
    Thanks for the help
    Attachments:
    new_control_and_acquisition_program.vi ‏946 KB

    got these vi's off ni site a while ago - see if they help
    Attachments:
    Memory_Monitor.zip ‏132 KB

  • How do i create arrays more efficientl​y

    how do i create arrays more efficiently?... need to insert data into an array every loop iteration.  I am using the insert into array function and shift registers.  I have been told this is inefficient... and they are right because I can make the program stop and it gives me the error that labview does not have enough memory.  A coworker said to avoid shift registers and investigate property nodes... i am new at this and don't know what this is exactly.. is there a tutorial on efficent data handling.. or examples that would be usefull? 

    tbob wrote:
    Mark is absolutely correct.  Indexing will take a lot of memory and a lot of time when the array gets large.  Indexing is great for small arrays though. With indexing, the first loop iteration will create an array of size 1.  On the next loop, the array has to be copied, re-dimensionedcto size 2, old data copied to new array, and new data inserted at end.  You can see how this will become a monster with an array size of 10,000.  With pre-allocation, there is no re-dimensioning and no copying.  Array elements are simple replaced.  And yes you do have to know the upper limit to use pre-allocation.  Kudos to Mark for pointing out the facts.
    A bit of a clarification is needed. Folks from NI told me that LabVIEW tries to be smart when autoindexing. When they allocate the memory the initial chunk of memory it is larger than a single element. So you don't necessarily get a copy and allocation every iteration. They may have improved the optimization to preallocate the complete array if the size is already known (a constant wired to the loop count). However, they would have no way of doing this if the number of iterations is not known ahead of time.
    Mark Yedinak
    "Does anyone know where the love of God goes when the waves turn the minutes to hours?"
    Wreck of the Edmund Fitzgerald - Gordon Lightfoot

  • Does anyone know how to display (in LabVIEW) the memory use during execution of an image and data acquisition VI to predict when it is time to cease the acquisition to prevent the program crashing?

    Does anyone know how to display (in LabVIEW) the memory use during execution of an image and data acquisition VI to predict when it is time to cease the acquisition to prevent the program crashing?
    I am acquiring images and data to a buffer on the edge of the while loop, and am finding that the crashing of the program is unpredictable, but almost always due to a memory saturation when the buffers gets too big.
    I have attached the VI.
    Thanks for the help
    Attachments:
    new_control_and_acquisition_program.vi ‏946 KB

    Take a look at this document that discusses how to monitor IMAQ memory usage:
    http://digital.ni.com/public.nsf/websearch/8C6E405861C60DE786256DB400755957
    Hope this helps -
    Julie

  • How to zip the folder in application server?

    how to zip the folder in application server?

    You can use
    open dataset with filter
    link:[http://help.sap.com/abapdocu_70/en/ABAPOPEN_DATASET_OS_ADDITION.htm#!ABAP_ADDITION_2@2@]

  • How to zip a text file and send as email attachment in SAP version 4.6c?

    Hi Guru,
    How to zip a text file in SAP version 4.6c which doesn't have class CL_ABAP_ZIP?
    Please help.
    Thanks & Regards,
    Ari

    Hi,
    Try this link
    [http://sap.ittoolbox.com/groups/technical-functional/sap-dev/sapr3dev-zip-file-from-sap-1707099?cv=expanded]
    Cheers,
    Surinder

  • Okay so my 64G iPhone 4S was stolen about a year and a half ago, it was backed up to my computer and I got a new iPhone but it's only 32G so I didn't have enough space to back it up as my old phone, so how do I get all my memory on my old phone back?

    Okay so my 64G iPhone 4S was stolen about a year and a half ago, it was backed up to my computer and I had used around 50G. I got a new iPhone a year ago but it's only 32G so I didn't have enough space to back it up as my old phone, so how do I get all my memory on my old phone back without having to get a 64G phone? Thanks

    No, there is no way to put more content then there is capacity for on the device.  As wjosten mentioned, you might be able to edit that backup to remove things to reduce it in size where you could then restore it to the new device, but obviously, everything that you had on the old iPhone will never fit on the new one.
    No matter what you do, you are going to end up with less content on the new device then on the old one - there is no way around that fact other than returning the 32GB iPhone and getting a 64GB one.  A gigabyte of data is a gigabyte of data, regardless of device, and will require a gigabyte of storage again, regardless of device.

  • How to reduce "other" in itunes memory on iphone?

    how to reduce "other" in itunes memory on iphone?  The amount of memory is increasing to point i have no more memory.  Most of this is in iphone "other".  How do I reduce this?

    Not if you sync regularly. And have set up the phone to sync to contact and calendar apps on your computer as explained in the manual.

  • How to zip multi files into one zip file in ABAP

    hi all:
    As you know, ABAP has CL_ABAP_GZIP class to support zip file function. But it only support one file zip. If I want to zip two files into one zip file, how to do it?
    thanks.

    Hi,
    <li>If you know how to zip one file,  to zip two files , you can follow the below steps.
    DATA:g_zipper     TYPE REF TO cl_abap_zip.
    "create our zipper object
    CREATE OBJECT g_zipper.
    "add 1st file to zip
    CALL METHOD g_zipper->add
       EXPORTING
         name    = file_name
         content = content_x.
      "add 2nd file to zip
    CALL METHOD g_zipper->add
       EXPORTING
         name    = file_name
         content = content_y.
    "save zip
    CALL METHOD g_zipper->save
       RECEIVING
         zip = zip.
    Thanks
    Venkat.O

  • How to zip a whole catalog with Java?

    can you tell me how to zip a whole catalog?
    i am not able to find a method that can zip a whole catalog and the files in it.
    please help me,thanks

    The following program is an extension of the code posted at:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=48084
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream ;
    import java.util.zip.CRC32;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    import java.util.Date;
    public class NZipCompresser {
       private String m_basePath=null;
       private File m_dir = null;
       private String m_OutputFileName;
       public static void main(String[] args) {
          File directory=new File(args[0]);
          new NZipCompresser(directory, args[1]);
       public NZipCompresser() {
       // This class gets directory and compress it reqursivelyinto zip file named outputFileName
       public NZipCompresser(File directory,String outputFileName) {
          m_dir = directory;
          m_OutputFileName = outputFileName;
          try {
             compress();
          } catch (Throwable e) {}
       public void compress () throws Exception {
          try {
             FileOutputStream zipFilename = new FileOutputStream(m_OutputFileName) ;
             ZipOutputStream zipoutputstream = new ZipOutputStream (zipFilename);
             m_basePath = m_dir.getPath();
             CompressDir (m_dir,zipoutputstream);
             zipoutputstream.setMethod(ZipOutputStream.DEFLATED);
             zipoutputstream.close();
          catch (Exception e) {
             throw new Exception ("Something wrong in compresser: " + e);
       public void setDirectory (File dir) {
          m_dir = dir;
       public void setOutputFileName (String FileName) {
          m_OutputFileName = FileName;
       public File getDirectory () {
          return m_dir;
       public String getOutputFileName () {
          return m_OutputFileName;
       //Walker through directory structure
       private void CompressDir (File f, ZipOutputStream zipoutputstream) {
          System.out.println(f);
          if (f.isDirectory()) {
             File [] files = f.listFiles();
             for (int j=0;j<files.length;j++) {
                if (files[j].isDirectory()) {
                   System.out.println("calling CompressOneDir with:"+files[j].getPath());
                   CompressDir (files[j],zipoutputstream);
                if (files[j].isFile()) {
                   System.out.println("adding file:" +  files[j].getPath());
                   addOneFile(files[j],zipoutputstream);
          System.out.println("exiting:"+f);
       //Actualy compress the file
       private void addOneFile (File file, ZipOutputStream zipoutputstream) {
          ZipEntry zipentry = new ZipEntry(file.getPath().substring(m_basePath.length()+1));
          FileInputStream fileinputstream;
          CRC32 crc32 = new CRC32();
          byte [] rgb = new byte [1024];
          int n;
          //Compute CRC of input stream
          try {
             fileinputstream = new FileInputStream(file);
             while ((n = fileinputstream.read(rgb)) > -1) {
                crc32.update(rgb, 0, n);
             fileinputstream.close();
          catch (Exception e) {
             System.out.println("Error in computing CRC:");
             e.printStackTrace();
          //Set Up Zip Entry
          zipentry.setSize(file.length());
          zipentry.setTime(file.lastModified());
          zipentry.setCrc(crc32.getValue());
          //Write Data
          try {
             zipoutputstream.putNextEntry(zipentry);
             fileinputstream = new FileInputStream(file);
             while ((n = fileinputstream.read(rgb)) > -1) {
                zipoutputstream.write(rgb, 0, n);
             fileinputstream.close();
             zipoutputstream.closeEntry();
          catch (Exception ex) {
             System.out.println("Error in writing data:");
             ex.printStackTrace();
    }Compile this program and run it as follows:
    java NZipCompresser directory_name zipOutput_name
    Is this what you're looking for?
    V.V.

  • How to Zip server files(in a directory) and throwing back to client

    Can someone tell me how to zip all the files in a particular directory of my web application and throwing back to client to give option for save that zipped file.
    i have used the below used function to zip in servlet. But I cann't zip a particular directory of my application in server.
    Please help
    private void zipDir(String dir2zip, ZipOutputStream zos)
    try
    //create a new File object based on the directory we
    // have to zip File
    File fileDir2Zip = new File(dir2zip);
    System.out.println("is directory ..."+fileDir2Zip.isDirectory());
    //get a listing of the directory content
    String[] dirList = fileDir2Zip.list();
    for(int i = 0 ; i < dirList.length ; i++)
    System.out.println("Dir list ..."+dirList);
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    //loop through dirList, and zip the files
    for(int i=0; i<dirList.length; i++)
    File f = new File(fileDir2Zip, dirList[i]);
    if(f.isDirectory())
    //if the File object is a directory, call this
    //function again to add its content recursively
    String filePath = f.getPath();
    zipDir(filePath, zos);
    //loop again
    continue;
    //if we reached here, the File object f was not
    //a directory
    //create a FileInputStream on top of f
    FileInputStream fis = new FileInputStream(f);
    //create a new zip entry
    ZipEntry anEntry = new ZipEntry(f.getPath());
    //place the zip entry in the ZipOutputStream object
    zos.putNextEntry(anEntry);
    //now write the content of the file to the ZipOutputStream
    while((bytesIn = fis.read(readBuffer)) != -1)
    zos.write(readBuffer, 0, bytesIn);
    System.out.println("Last");
    //close the Stream
    fis.close();
    catch(Exception e)
    e.printStackTrace();

    Hi
    Whatever file exist in directory should execute with out any condition because they delete all files after exection of my process chain. They are using another process chain once a day for deletion of all files in that directory.
    So directory always contains fresh files.
    Here by using routine in infopackge i am unable to run all files one by one at a time.
    As of my knowledge  by using infopackage we can run only one file at a time.
    Here my question is , can we run more than one file in single run of infopackage?
    or can we have any function module to run the infopackage from externally? so that i can use function module in abap program? any ideas..
    Regards
    Raju

  • How to Zip Excel files using File Adapter?

    Hi,
    We have tried to ZIP the Excel file with  PayloadZipBean in File adapter. But we faced some issue while zipping.
    We have seen some zunk data in excel file after zipping with PayloadZipBean. Someone please help how to zip Excel files in PI with File Adapter.
    Regards,
    Sreeramulu Konjeti.

    Hi Sree,
    If you are facing any issue with PayloadZipBean then you can use java mapping to Zip the files.
    Please find the complete Java mapping code to zipt the file
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/50ce0433-4309-2b10-4bb4-d421e78463f7?quicklink=index&overridelayout=true

  • How to map an array to fixed fields using Biztalk mapper

    I need to remap an array of objects like this:
        <Root>
          <ListOfObjs>
            <Obj>
              <Attr1>0000</Attr1>
              <Attr2>Hello!</Attr2>
            </Obj>
            <Obj>
              <Attr1>1111</Attr1>
              <Attr2>Hello1!</Attr2>
            </Obj>
          </ListOfObjs>
        </Root>
    in an output like this:
            <Root>
                <Obj1_Attr1>0000</Obj1_Attr1>
                <Obj1_Attr2>Hello!</Obj1_Attr2>
                <Obj2_Attr1>1111</Obj2_Attr1>
                <Obj2_Attr2>Hello1!</Obj2_Attr2>
            </Root>
    So in my XSD schema I have something like this:
    Schema Input
                               <xs:element name="Root">
                                <xs:complexType>
                                 <xs:sequence>
                                  <xs:element name="ListOfObjs">
                                   <xs:complexType>
                                    <xs:sequence>
                                     <xs:element name="Obj">
                                      <xs:complexType>
                                       <xs:sequence>
                                        <xs:element name="Attr1">
                                         <xs:simpleType>
                                          <xs:restriction base="xs:string">
                                           <xs:minLength value="1"/>
                                           <xs:maxLength value="50"/>
                                          </xs:restriction>
                                         </xs:simpleType>
                                        </xs:element>
                                       <xs:element name="Attr2">
                                        <xs:simpleType>
                                          <xs:restriction base="xs:string">
                                           <xs:minLength value="1"/>
                                           <xs:maxLength value="50"/>
                                          </xs:restriction>
                                         </xs:simpleType>
                                        </xs:element>
                                       </xs:sequence>
                                      </xs:complexType>
                                     </xs:element>
                                    </xs:sequence>
                                   </xs:complexType>
                                  </xs:element>
    Schema output
                                     <xs:element name="Root">
                                      <xs:complexType>
                                       <xs:sequence>
                                        <xs:element name="Obj1_Attr1">
                                         <xs:simpleType>
                                          <xs:restriction base="xs:string">
                                           <xs:minLength value="1"/>
                                           <xs:maxLength value="50"/>
                                          </xs:restriction>
                                         </xs:simpleType>
                                        </xs:element>
                                       <xs:element name="Obj1_Attr2">
                                        <xs:simpleType>
                                          <xs:restriction base="xs:string">
                                           <xs:minLength value="1"/>
                                           <xs:maxLength value="50"/>
                                          </xs:restriction>
                                         </xs:simpleType>
                                        </xs:element>
                                        <xs:element name="Obj2_Attr1">
                                        <xs:simpleType>
                                          <xs:restriction base="xs:string">
                                           <xs:minLength value="1"/>
                                           <xs:maxLength value="50"/>
                                          </xs:restriction>
                                         </xs:simpleType>
                                        </xs:element>
                                        <xs:element name="Obj2_Attr2">
                                        <xs:simpleType>
                                          <xs:restriction base="xs:string">
                                           <xs:minLength value="1"/>
                                           <xs:maxLength value="50"/>
                                          </xs:restriction>
                                         </xs:simpleType>
                                        </xs:element>
                                       </xs:sequence>
                                      </xs:complexType>
                                     </xs:element>
    In addiction I have to evaluate every single value because when I found some conditions (like if value=0000 output should be NULL).
    What would be the best way to do it? I'm thinking to develop a custom functoid but I'm not sure it would be the best way, probably it could be done even using XSLT inline transforms, can you point me in the best direction?
    Thank you

    Hi,
    You cannot directly map an array output to any single field in BizTalk mapper.
    Couple of options :
    1) create
    the Xslt or inline C# code
    Refer: 
    http://seroter.wordpress.com/2008/10/07/splitting-delimited-values-in-biztalk-maps/
    2) Shankycheil has
    provided a solution to similar requirement in the below link, u can also refer that.
    https://social.msdn.microsoft.com/Forums/en-US/55ec472d-4f34-4057-b1c6-0e50740f0f6e/how-to-itterate-string-array-values-in-biztalk-mapper?forum=biztalkgeneral
    Rachit
    Thank you, I already seen both posts, but I'm not sure they are what I need or I can't understand well how to use them.
    Speaking about the first solution, as I told before, in the example I should have an array already formed and delimited by a char (something like "obj1attr1-obj1attr2-ob2attr1-obj2attr2". In this situation probably this example could be a good
    point to start from, but how to transform my complex input object in a similar formatted string?
    About the second I don't understand well what is the working solution that they have adopted. Is the 4 steps solution suggested by  Shankycheil? If yes, how can I loop between all array elements and extract all their values?

  • How to ZIP a PDF File with a Password Protection

    Hi,
    i've a pdf file with created smartforms and i want to assign a password to that pdf file but the SAP doesn't let doing that protection. So i want to create a zip file with a password protection for PDF file.
    How can i create a zip file with a password protection? Can somebody help me please?
    Thanks.

    Hello,
    Check this links
    Take a look to the class CL_ABAP_GZIP
    open (top-)zip-archive
    CALL METHOD lo_zip->load
        EXPORTING
          zip             = lv_zip_file_head
        EXCEPTIONS
          zip_parse_error = 1
          OTHERS          = 2.
    create sub-zip-archives which contain the files you would assign to a folder
    add sub-zip-archive to top-zip-archive
    CALL METHOD lo_zip->add
         EXPORTING
            name    = lv_zip_filename
            content = lv_zip_file.
    save zip-archive
    CALL METHOD lo_zip->save
        RECEIVING
          zip = ev_zip_file.
    ABAP Development
    How to ZIP a PDF file email attachment
    Re: How to ZIP a PDF file email attachment

Maybe you are looking for