掲示板

Download Zip with PDF inside

thumbnail
7年前 に Borja Blázquez によって更新されました。

Download Zip with PDF inside

Junior Member 投稿: 30 参加年月日: 13/10/07 最新の投稿
Hi,

I am trying to download a zip file which includes a pdf file inside. i can obtain the bytes for the pdf file but i can not include it inside a zip file and download it.
I try doing this in several ways, For example:


ByteArrayOutputStream os = getPDF(request, response,list); // Call the function to generate the PDF

final ZipWriter writer = ZipWriterFactoryUtil.getZipWriter();
writer.addEntry("file.pdf",os.toByteArray());
response.setProperty("Content-Disposition", "attachment; filename=myarchive.zip");
response.setContentType("application/octet-stream");
 byte[] archive = writer.finish();
 response.getPortletOutputStream().write(archive);


getting the error de.schlichtherle.io.ArchiveController$ArchiveFileNotFoundException: /xxxxxxxx/tomcat-7.0.62/temp/a30a020b-4e67-4b5d-a6cf-d7f4cc49883b.zip (cannot read virtual root directory)
Even with the error it is generating the zip file but i can not open it.

Other way:


ByteArrayOutputStream os = getPDF(request, response,list); // Call the function to generate the PDF

response.setProperty("Content-Disposition", "attachment; filename=myarchive.zip");
response.setContentType("application/octet-stream");
ZipOutputStream zos = new ZipOutputStream(os);
zos.setLevel(9);

ZipEntry zipEntry = new ZipEntry( "pdf.pdf" );
zipEntry.setSize(os.toByteArray().length);
zos.putNextEntry(zipEntry);
zos.write(os.toByteArray());
zos.closeEntry()ar



getting no error but generating the zip archive with an error while opening.

Any idea or a clue to get it?

Thanks in advanced.
thumbnail
7年前 に Olaf Kock によって更新されました。

RE: Download Zip with PDF inside

Liferay Legend 投稿: 6400 参加年月日: 08/09/23 最新の投稿
Borja Blázquez:
I am trying to download a zip file which includes a pdf file inside. i can obtain the bytes for the pdf file but i can not include it inside a zip file and download it.
I try doing this in several ways, For example:
...


Where are you doing this? ActionHandler? ResourceHandler? Servlet?

Also: Check permissions on the temp directory. In case you once ran tomcat as root but don't do any more, you might have weird permissions and ownerships on the temp- and work directory.
thumbnail
7年前 に Borja Blázquez によって更新されました。

RE: Download Zip with PDF inside

Junior Member 投稿: 30 参加年月日: 13/10/07 最新の投稿
Hi,

thanks for your answer!

I am doing this in a resource method. May be i forgot some configuration?

i set full permissions to temp folder but i got the same error.

Thanks!
thumbnail
7年前 に Borja Blázquez によって更新されました。

RE: Download Zip with PDF inside

Junior Member 投稿: 30 参加年月日: 13/10/07 最新の投稿
Liferay reported the solution for this issue:

It is necessary to invoke zipwriter.getFile()to write the file correctly. This method sends the zip file to disk using the zipwriter library.

the implementation of the method zipwriter.finish() in liferay 6.2 is not correct because it is not calling getFile() causing the exception reported. we did not detect it because this method is not used in Liferay.

For liferay 7.0 it is rewrite to work fine.


    public byte[] finish() throws IOException {
        java.io.File file = getFile();

        return FileUtil.getBytes(file);
    }


If you need to call finish() to get the bytearray of the file, you can do this: FileUtil.getBytes(zipwriter.getFile());


And it wokrs...!

thanks!
thumbnail
6年前 に Gaurav Jain によって更新されました。

RE: Download Zip with PDF inside

Junior Member 投稿: 85 参加年月日: 16/07/12 最新の投稿
Hi Borja Blázquez,
I am trying the same thing. Having some files in a zip and want to get it force-downloaded in browser. Can you please tell me which response object are you using?
I am using HttpServletResponse in following code
final ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
String fileName = "my_file.zip";
String contentDispositionFileName = "attachment; filename=\"" + fileName + "\"";
response.setHeader("Content-Disposition", contentDispositionFileName);
response.setContentType("application/zip");// here I also tried [b]application/octet-stream[/b] and [b]application/force-download[/b]
File file = zipWriter.getFile();
byte[] bytes = FileUtil.getBytes(file);
zipWriter.addEntry(fileName, bytes);
byte[] archive = FileUtil.getBytes(zipWriter.getFile());
response.flushBuffer();
response.getOutputStream().write(archive);


I am running above code in Process Action Method of the portlet. On executing this process action method I get following response in browser and file also does not get downloaded
PK \M�J!�Vy8�� �� KC )��`fhHd Y� �%4 "���I�MBY��6�diÍ �,� c-QH�l�J ?u�+��ӽ�{��y�x��}��~����9X� @ ���h�� ���x�� ��ད d�@ 9��-y��R_���� g{�� g袌 &z ��,�u�1�E�� ��ioz��y�� 2emoticon_3�g�� ���zG�,�I��� }yʂ� \�H����)��|Y -�u g� �oGpe�������B� $3 ō�! ��=|I��� �� ɩ���6�ܩ�P�L��Fj�G{rG$W� D�QWܽ/ � �dv�.�&κ�=� :2:��%p�� ��2� F � `��{͘�� �/1�B��=q�� �l�;7 �3�����IR��w�$5xu: p6�� �3;XPX� ��ʱ�%��� �����+�6F s3�I:�� )HLi �*��q~�V�F*_��3��� �&�^'u��` �鷝X����� BAl@% ����T�Ts2�ɬ %@-�q� p���� �-9 �.�o/�a���SY��a�� � sm�PwJ�O`� f�6T���A��l�ž^�76Q��sy4 `z#� x^�U �%߱6�`*��K�ۆ� [8� � � 2��� ~�C��q�z�f�� N!���Ѕ׹�-}썯8�˨�i'>�~ Zcy��� 8 zd{7 W��q�n�vL�ީ}�l �n dS�IQ�rhFiN 쌻 ��(� =v�㮟���\c!X&) ��c�Ilk��ͼv w0:���ߤ�� W�#=�#�/.Y��u�Y[k��x��ހ�� �� �o.��� )� �Y z� ���- � ��o ��K �H��.��<5 z� ����A׊�{��l x� ��k� |� �K�}��3 i u�?{Y��9J��{8�)��j� ��� � � F~ei�Ľ)�7�B.}C�Ӝ� �El� ��'��s�*hu� �mq��2J Je��3�ņv� R��{�#G<�d�q�"�4<ט��;+� ���l�y5��� PZ��=Gq��� ���&k�|� � �95�+| l`(� ��o"G��>9j��CR��� ����|�ĺ ���}� � �ix٫�^��+�Z�� ��� � �¥� � � �� h�EEǃ�g���v���1���1e� v�ƌ�� ��� �� ��kI~���ge��R�# ���@���t���/ڡ_k� +�ڍ��;�:ϴ� Jx+}_�)M�ķ���&��7| @6X�d�?���e�M�m �|�QP��� � �`I� ��� ���C�m� �{��~ V����T �����-/�� h° O]�d u�[ �� � � Ml'�K�4� l� �2n`�x#.������m�R{��Dn�Ѩr�*vi@,� n�/�:�q ޽띪 U� ��9vͣ�yE40�b�P�~�C� �yZ� ��l��6h��-0h�˵ ��(�� ���}�>�� �cnr�A��� ro��x ���^2g�� jS �9�VG���[� �c �p?�������24����VCo�N5+����y���+SB����f;a dg�� fb�����e#�ubZP�h�|�{ >� �v)2zL� ��c�oi =���Hnn`���R �c�W��*�L [h= ���]�D�յ]�Ζ ��{ �<3��ȂQ�U&2FFGH�� �Ø�v�������ӻTE̕RO�{'l� �P�*�� + ����(z� h8�R�K�|ޭ��X�()���k�:''v ��A��6�� � 9 �� �;g�Aە ���d,h � a����� �`��(�J�nJ ��e��y�#��R�!�����K�; M�nL[�ˬ��wgw D� $J�����d�=�<%�'f���̲X�*�ݺ#� � �eLBZ �<�NX��?O� �v� )���x �\R����A}�2an?Hѽ � ኊ��'� �:us�5BZ� TO~�ɗ0��> 4��t �R��Ԧ[ r^� 3C�� �| �!��#�6� *�c�e�ӤH���G� M�(��1�ҏi��u��wks� l���� vkG� ���7J�vحK � l��߭;۱��%@���co�ʟ�H���}� v�� �e}�ߍ���[-"� 6 �obb;����'�o�_; k ���n �lc�y�ͷ PK uw �� � PK [M�JJŢ w � )ՕAO�@ ���+ֽc 1JҖ�^L� D/���;� ���.��׻[ & I�&�y���M�4u�y$� ��+���� ��ͦ�(� �� � l ��ԣ(�/ϔ0 a%t�oe��pY!!�A F(5a�aT�f�G0GJB��=B�i ��� ��K��fl�E� FI����~��de�&�<�۠�u熒 �=IQx4 �� b4 K�Nz[� �NH *{� �m�. �"��cƙ^x� �p#�вg��k�[scRTń ��{6�� � ��d�N9�o�M- �D� �4�� ri ;�:����� �aB3� �< �p�j�zW� ,g*��G��4"e�_������TF���P�N�[/ m��  �S6�/1}���r2�N�wY?��y�1 �c�J� PK [M�Jl�M(� b )�]K�0 ���_ s_�:a � �7��� 9kζ`��4�Z �IW��`0��xWN�yϓ> �D�%�+ ��� F�v��2 �� L zB� 0 )���+J Na!LU )G�V� �= ��4���q�ڞ�R AR�%�|]{�b�4�� �OnYH�^ %6�>��S�~�O�F`�R !���� R� ʩ���� Y]��ju /��\ �1~���� gf R�� 4�9h�q6�֞H �p�4 �nON��Ƿ ��] tʾ͑�r�Kd7 ,��pɺm���M��ΠSS; � �k� ��G�r �®�R�my ';�:sAݣA�YO ī U�~Nӏ ʌ�r���1��~�r��5,��d����, !�*�� �� ����opҼ�?�K�3ԛ� c;�c�(���dQ���IW �/� t�OD� PK [M�J��o� � .M�MO�@ ��� � 4E��P /�~��� � �F(�Z�^�� {w��$�2]�8Io���PK [M�J���� � .U�=o�0 ���W� "T��B��.l Zu�.�%�p�ԾP"���#�*L������$��; P:�d ��� S� }� ؁� \/����d� �� � ��P �}_r�)Q�� ��f� � :�dΦ_�.�f�*�o[ge �5 �B��zv ;CL��� ��ڜ ZѢ�B�=���� x�m ���D�q�Mt 1� cm �n��nvv9�����~F��|����i'�iȧ�U��~T� �\e�m� o�]� E |���qZ>>��4 ,r5=�ґ?��kz!���� PK [M�J���,Y I Ք�N�0 ���)��!M pqRU��@�RUh o�����i ��u� ��#D��f�wdz�I Õ�l�E��4��Q� �^O Yf ,Ai�k�t ��2����p&1�J�N��Q�@�& �b 5 L-�`a��TC'�,Q�� ,s��* g*��j� ���Br��V����ۢ�^r��|����� A^�� y �D'�H�[��S� �[�\d�%�Qo\m�N �MX+i !/ h�F: -�K`;��-MY4]6�y$ ����d4uYg�H������a�R�w�)�k�g]V� V ������� QV��� cE ��/pj����5�G�(��;�����.����$�/* �u ;P?e� �� ���� PK [M�J� � P .lm�OO�@ �� ��B1jB�Ą & � �f�N����� � ���5�$�iv�{���y/��+e�[��x�w =9���qT���� ۰�NPJR���JiX� ��3����}SS"ij"t ��**=�А ��F�[� ��� �t �XK� �D�7޲�S� $O�� ܣT�h8� 2 ��n9���۝4 D�c��8^�y ���� -�Ko߸@X@E�S��J�|�� �-�!� ��R0A�0��/a��տpz�>�_p�� ��%!���� �w�KS� � m6 h�@ ��L�s� ��Q��$�c 1L*u�k��AcE �:_�*����&���)�1'�� �R����� � PK [M�J� 7��� ��͊�0 F�}�����4in .T�"88Ժ��? t*����� +��; N�ۯW�ӡk����~�pq �|_��-.�@��杙���;�� ����X� ��%,kKD:Z0� � l� ��$�#+Ƚ��?\� 9�N.�aT"1h� (c ȸ �Dh� ����Ͼ�>u����0�A/syx 3�N2� y� �O�Zz �<�d# � � PK [M�J��ר�- m�M �@ �ὧ� �L��х��c�O� ����[ Rܾ_�!$=R�ږc�>a֑�X�P�5<�%���u^��kk��8�y�LY�y@� ${ ���*�e'�`����p� �c5\�%�!H� ��2��c.l#y������e��~) ��Ɗ� P #��e�� �� �Y I� � PK ? \M�Juw �� � � �
thumbnail
6年前 に Christoph Rabel によって更新されました。

RE: Download Zip with PDF inside

Liferay Legend 投稿: 1554 参加年月日: 09/09/24 最新の投稿
Where are you doing that? In an action in a portlet? Or in doView()? You should only do something like that in a servlet or in a serveResource method.

It that's all fine:
Idea 1:
The headers are somehow wrong. You can check in the browser with F12 and look at the network request what you receive and compare it to a correct one.

Idea 2: Your file is actually zipped twice. I had this once with a misconfigured reverse proxy. It zipped the file a second time and the browser unpacked it and showed the contents.
thumbnail
6年前 に Olaf Kock によって更新されました。

RE: Download Zip with PDF inside

Liferay Legend 投稿: 6400 参加年月日: 08/09/23 最新の投稿
Gaurav Jain:
I am using HttpServletResponse in following code
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);


I am running above code in Process Action Method of the portlet. On executing this process action method I get following response in browser and file also does not get downloaded


Two issues:
  • The main issue: ProcessAction is the wrong place to do this.!
  • PortalUtil.getHttpServletResponse(actionResponse); does not give you the original request, but some adaptation of the PortletResponse to the HttpServletResponse interface.

You only need to work on the first issue and completely forget about PortalUtil.getHttpServletResponse. In most cases it is not required for a proper solution.

Move your implementation to the resource serving phase. A ResourceResponse allows you to set headers and stream data. Action is just plain wrong, even if you somehow can get it to work: It's incorrect and not guaranteed to work.
thumbnail
6年前 に Gaurav Jain によって更新されました。

RE: Download Zip with PDF inside

Junior Member 投稿: 85 参加年月日: 16/07/12 最新の投稿
Thanks Christoph Rabel, Olaf Kock for your valuable responses,

I updated my code and now I am doing that in serveRresource of the portlet. Now improvement is that I am not getting that encoded response in browser screen, but file is still not getting downloaded. I modified the permissions of temp folder as well but no success. One strange thing I noticed that If I configure the Internet Download Manager(IDM), file gets downloaded.

And my code is as follows:

1. view.jsp

&lt;%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %&gt;

<portlet:defineobjects />
<portlet:resourceurl var="downloadResource">
</portlet:resourceurl>
<aui:button type="button" onClick="downloadResources();" value="Download Resources" />
<script type="text/javascript">
function downloadResources(){
    AUI().use('aui-io-request', function(A){
        A.io.request('${downloadResource}', {
               method: 'post',
               data: {
            	   <portlet:namespace />param: 'value', 
               },
               on: {
                   	success: function() {
                    alert("Requested resources have been downloaded" );
                   }
              }
        });
     });
}
</script>


2. portlet class's serve resource method

final ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();
createFile(_JOURNAL_DDM_STRUCTURE_PROPERTY_FILE_NAME, structureProperties.toString(), zipWriter);// creating file here adding them in zipWriter
String fileName = "resources-export" + "-" + Time.getShortTimestamp() + ".zip";
String contentDispositionFileName = "attachment; filename=\"" + fileName + "\"";
resourceResponse.setProperty("Content-Disposition", contentDispositionFileName);
resourceResponse.setContentType(ContentTypes.APPLICATION_ZIP);
resourceResponse.flushBuffer();
resourceResponse.getPortletOutputStream().write(FileUtil.getBytes(zipWriter.getFile()));


What is the thing that I am doing wrong here?

Thanks and regards,
Gaurav Jain
thumbnail
6年前 に Christoph Rabel によって更新されました。

RE: Download Zip with PDF inside

Liferay Legend 投稿: 1554 参加年月日: 09/09/24 最新の投稿
The question is, is your frontend code or your backend code the problem.
Could you try to test the generated pdf url with wget or curl? Or directly by making it clickable as a href link.

You get the file -> Problem is in AUI script
You don't get a file -> Still backend problems
thumbnail
6年前 に Gaurav Jain によって更新されました。

RE: Download Zip with PDF inside

Junior Member 投稿: 85 参加年月日: 16/07/12 最新の投稿
Hi Christoph Rabel,

Christoph Rabel:

Could you try to test the generated pdf url with wget or curl? Or directly by making it clickable as a href link.


Sorry, but I did not get your point. Can you explain bit more about generated pdf url.
BTW FYI, I am having some xml and properties file in the zip, which I am trying to make force- downloaded. And how can I get that URL to use with wget or curl?

Regards,
Gaurav
thumbnail
6年前 に Christoph Rabel によって更新されました。

RE: Download Zip with PDF inside

Liferay Legend 投稿: 1554 参加年月日: 09/09/24 最新の投稿
It's the content of the following variable.
${downloadResource}

Just copy the url in the browser. e.g. by looking into the sourcecode or in the developer console. You can then try to open the url in the browser or fetch it using wget or curl or something like that.